Skip to content
Related Articles
Open in App
Not now

Related Articles

Java | Class and Object | Question 4

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 28 Jun, 2021
Improve Article
Save Article
Like Article

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
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!