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)); } }

25th Sep 2020, 3:29 PM
Gaurav Rawat
Gaurav Rawat - avatar
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
25th Sep 2020, 3:40 PM
chaithra Gowda
chaithra Gowda - avatar
+ 1
Isn't 9+6 = 15 ? Make the add() method static in class A.
25th Sep 2020, 3:33 PM
Avinesh
Avinesh - avatar