0
What is the output and why?
public class A{ int add(int i, int j){ return i+j; } } public class Program extends A { public static void main(String[] args) { short s = 9; System.out.println(add(s,6)); } }
2 Answers
+ 4
output will be 15 if u fix error!
>non-static context cannot be accessed in static method.
>Static methods can be accessed in static method only.
Since ,main method is a static method, we have to use static method or instance reference to that method,
But you declared a non static add() method and called that in main method so it's throwing an error.
Hope this helps you to understand
https://code.sololearn.com/cR2m73QLG3mt/?ref=app
+ 1
Isn't 9+6 = 15 ?
Make the add() method static in class A.