Skip to content
Related Articles

Related Articles

Java | Class and Object | Question 4
  • Difficulty Level : Medium
  • Last Updated : 11 Jan, 2016

Predict the output of following Java program.




class demoClass
{
    int a = 1;
  
    void func()
    {
        demo obj = new demo();
        obj.display();
    }
  
  
    class demo
    {
        int b = 2;
  
        void display()
        {
            System.out.println("\na = " + a);
        }
    }
  
    void get()
    {
        System.out.println("\nb = " + b);
    }
}
  
  
class Test
{
    public static void main(String[] args)
    {
        demoClass obj = new demoClass();
        obj.func();
        obj.get();
  
    }
}


(A)

a = 1
b = 2

(B) Compilation error

(C)

b = 2
a = 1


Answer: (B)

Explanation:
Members of inner class ‘demo’ can not be used in the outer class ‘Test’. Thus, get() of outer class can not access variable ‘b’ of inner class.

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :