Access modifiers for inner classes

Possible duplicate:
protected / public inner classes

I'm sure the question has already been asked, but I could not find it, so I will ask ...

I am curious what is the difference between a private (protected) and a public inner class. I can use both of the class containing the class using an external class object.

public class A{
   private class B{

   }

   public static void main(String[] args){
     A a = new A();
     B b = a.new B();
   }
}
+3
source share
2 answers

Access to a private inner class can be obtained within the class that defined it.

If you have another class, Bnot displayed:

public class C {
   public static void main(String[] args){
     A a = new A();
     B b = new B(); // compile error
   }
}
+6
source

Actually, you are still inside class A, since the main method is the static method of class A

+2
source

Source: https://habr.com/ru/post/1664051/


All Articles