5

Possible Duplicate:
protected/public Inner Classes

I am sure the question has been asked already, but I wasn't able to find one so I'll ask...

I am curious what is the difference between private(protected) and public inner class. I am able to use both from out of the containing class using the outer class object.

public class A{
   private class B{

   }

   public static void main(String[] args){
     A a = new A();
     B b = a.new B();
   }
}
Community
  • 1
  • 1
Headshota
  • 21,021
  • 11
  • 61
  • 82

2 Answers2

7

A private inner class can still be accessed within the class that defined it.

If you have another class, B isn't visible:

public class C {
   public static void main(String[] args){
     A a = new A();
     B b = new B(); // compile error
   }
}
Jeremy
  • 22,188
  • 4
  • 68
  • 81
3

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

Zak
  • 24,947
  • 11
  • 38
  • 68