0

I can understand why inner class can access outer class private members, but its not clear why instance of inner class defined in outer class method can access inner class private members. For example: the following code works fine.

    class OuterClass
   {
    int outer_default;
    private int outer_private;
    protected int outer_protected;
    static int outer_static;
    static {
        outer_static=5;
    }
    public OuterClass()
    {
        outer_default=1;
        outer_private=2;
        outer_protected=3;
        System.out.println("Outerclass object created!!!!");
    }
    public void outerMethod()
    {
        System.out.println("**********Outerclass accessing outer class members********************");
        System.out.println("outerdefault: "+outer_default);
        System.out.println("outerprivate: "+outer_private);
        System.out.println("outerprotected: "+outer_protected);
        System.out.println("outerstatic: "+outer_static);
        System.out.println("**************************************************************");
        InnerClass innerObj = new InnerClass();
        System.out.println("**********Outerclass accessing inner class members********************");
        System.out.println("innerdefault: "+innerObj.inner_default);
        System.out.println("innerprivate: "+innerObj.inner_private); //HOW IS THIS POSSIBLE??????
        System.out.println("innerprotected: "+innerObj.inner_protected);
        System.out.println("**************************************************************");
    }
    class InnerClass
    {
        int inner_default;
        private int inner_private;
        protected int inner_protected;
        public InnerClass()
        {
            inner_default=4;
            inner_private=5;
            inner_protected=3;
            System.out.println("Innerclass object created!!!!");
        }
        public void innerMethod()
        {
            System.out.println("**********Innerclass accessing inner class members********************");
            System.out.println("innerdefault: "+inner_default);
            System.out.println("innerprivate: "+inner_private);
            System.out.println("innerprotected: "+inner_protected);
            System.out.println("**************************************************************");
            System.out.println("**********Innerclass accessing outer class members********************");
            System.out.println("outerdefault: "+outer_default);
            System.out.println("outerprivate: "+outer_private);
            System.out.println("outerprotected: "+outer_protected);
            System.out.println("outerstatic: "+outer_static);
            System.out.println("**************************************************************");
        }
    }
}
class Main {
  public static void main(String[] args) {
    OuterClass outer = new OuterClass();
    outer.outerMethod();
    OuterClass.InnerClass inner = outer.new InnerClass();
    inner.innerMethod();
  }
}

I have commented near the code in which I have doubt.

mad_manny
  • 1,081
  • 16
  • 28
Tarun Mohandas
  • 765
  • 1
  • 8
  • 15
  • 1
    I can't tell which of three questions you're asking: 1) Why does the compiler allow it? 2) Why was the language designed this way? 3) How does it work in bytecode? If you could clarify which you mean, we can hopefully answer it (or find a duplicate, e.g. http://stackoverflow.com/questions/1801718) – Jon Skeet Mar 11 '16 at 06:40
  • 2
    Inner class is just a member of outer class , so it makes sense to allow such access. – Roshith Mar 11 '16 at 06:42
  • A inner instance class (one which is not defined as `static` acts much like a method of the outer class, which allows it access to instance fields and methods. The choice to allow this is a primary a design choice and one I can say I like – MadProgrammer Mar 11 '16 at 06:43

0 Answers0