ExamVeda
Login
Home
Data Types and Variables
?

What will be the output for the below code?
public class Test{
      int i=8;
      int j=9;
      public static void main(String[] args){
            add();
      }
      public static void add(){
            int k = i+j;
            System.out.println(k);
     }
}

Answer & Solution
Correct Answer: Option D

i and j are instance variable and attempting to access an instance variable from a static method. So Compilation fails.

Examveda
Question posted by Examveda
Community

Join the Discussion

2 Comments
Swapnali Mane
Swapnali Mane 6 years ago
Adding int k=i+j;
Syed Shajib
Syed Shajib 7 years ago
why can adding int k = i+j;