Inner class - GuangsZuo/Java GitHub Wiki

What I have learned from << Thinking in Java>>

  1. it is a coding-hidden mechanism.

  2. It has access to the outer class's member , even private member.

    When you create an inner class, an object of that inner class has a link to the enclosing object that made it, and so it can access the members of that enclosing object—without any special qualifications. In addition, inner classes have access rights to all the elements in the enclosing class.

  3. In outer class,you can use it just like other ordinary classes.

    But if you want to make an object of the inner class anywhere except from within a non-static method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName

  4. .this and .new tricks

If you need to produce the reference to the outer-class object, you name the outer class followed by a dot and this. Sometimes you want to tell some other object to create an object of one of its inner classes. To do this you must provide a reference to the other outer-class object in the new expression, using the .new syntax , like this:

  outerClass.InnerClass innerClassobj = outerClassObj.new innerclass();