ExamVeda
Login
Home
Constructors and Methods
?

What is the output for the below code?
public class A{
      int add(int i, int j){
            return i+j;
      }
}
public class B extends A{
      public static void main(String argv[]){
            short s = 9;
            System.out.println(add(s,6));
      }
}

Answer & Solution
Correct Answer: Option B

Cannot make a static reference to the non-static method add(int, int) from the type A. The short s is autoboxed correctly, but the add() method cannot be invoked from a static method because add() method is not static.

Examveda
Question posted by Examveda
Community

Join the Discussion

2 Comments
Anonymous
Anonymous 11 years ago
Answers should be B
Ramgopal Panda
Ramgopal Panda 11 years ago
above java program will give two error.
1. At line-1 or line-6(we can't define two public class in a java file)
2.line-9 (non static method is accessed from static method)