2

I have a use case, which I have simplified as the below program:

public class A {
   private int x = 100;

    class B {
       private int y = new A().x;
    }

  public static void main(String []s) {
    System.out.println(new A().new B().y);
  }
}

This program runs fine and prints 100.

As per the docs ( Access Modifiers ): private modifier makes sure that the field is accessible only within its own class. But in above program it seems to be contradicting the same.

Does this mean that?

  1. Inner class can access private members of outer class.
  2. Private variable of the inner class can be accessed in outer class.

Please help me understand.

5
  • 1
    Yes, 1 and 2 are correct, as you demonstrate here.
    – markspace
    Jan 22, 2021 at 18:20
  • To add a bit more, the inner class B is a member of class A, and therefore can access private members of A. Likewise since B is a member of A, the static method main (which is also part of A in this example) can access private members of B.
    – markspace
    Jan 22, 2021 at 18:23
  • But, Doesn't private modifier makes sure that the field is accessible only within its own class ?
    – Amit kumar
    Jan 22, 2021 at 18:25
  • B is within "its own class" here with respect to A. That's how the specification says it works.
    – markspace
    Jan 22, 2021 at 18:25
  • Thanku so much @markspace! I got the clarity now.
    – Amit kumar
    Jan 22, 2021 at 18:29

2 Answers 2

5

The Java Language Specification is a better source of precise information about the language.

In this case, the relevant part is in clause 6.6.1:

Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level type (§7.6) that encloses the declaration of the member or constructor.

That "within the body of the top level type" is the important part.

So basically, within the body of the same top level type (A in your case), all the code (whether written in a nested type or not) can access any private constructor/member declaration within the same body (whether declared in a nested type or not).

0
2

Does this mean that ?

Private variable of the inner class can be accessed in outer class.

Yes. The following works.

public class A {
    public void someMethod() {
        // this references the private field
        // in the inner class
        System.out.println(new B().x);
    }

    class B {
        private int x = 42;
    }
}    

Does this mean that ?

Inner class can access private members of outer class.

Yes. The following works.

public class A {
    private int x = 100;

    class B {
        public void someMethod() {
            // this references the private field
            // in the outer class
            System.out.println(x);
        }
    }
}

Please help me understand.

The examples above should help.

1
  • Thank you so much @Jeff. This is really helpful !
    – Amit kumar
    Jan 22, 2021 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.