Tuesday 6 January 2015

SCJP notes: innner classes

"Regular" inner classes
  • The inner class couldn't have anything static, except compile-time constants
  • The inner class doesn't initialized when the outer is initialized
  • The inner class could have every possible access modifiers
  • Actually, inner class could have all modifiers (like: strictfp, final, abstract)
  • The only way you can access the inner class is through a live instance of the outer class
  • So you couldn't instantiate the inner class in static method of the outer class(unless you create there an instance of the outer class)
  • The inner class as a member of the outer class has the access to all other members of the outer class
  • To instantiate the inner class inside outer, just write new Inner()
  • To instantiate the inner class outside outer, you need to write something like: new Outer().new Inner()
  • You can get the link to the outer class that corresponds to the inner by Outer.this
  • You can get the link to the inner class inside inner by this or Outer.Inner.this
  • You couldn't instantiate static member in the inner class
  • But you could inherit them from other class
Method-local inner class
  • The instantiation of the method-local inner class should go after its declaration
  • The instantiation of the method-local inner class could be done only inside the method where it is declared
  • As "regular" inner class has access to all outer class members
  • The method-local inner class could use only final local variables
  • The method-local inner class could have the following modifiers: strictfp, final, abstract
  • The method-local inner class could be declared in the static method, but in this case it will have access only to the static members of the outer class, and no any "this" will be available
  • As "regular" inner classes method-local inner class couldn't declare static content
Anonymous inner class
  • There could be new methods in the anonymous inner class, but there will be no way (except reflection) to call them
  • You couldn't define new constructor within the anonymous inner class
  • You could use the proper constructor by the anonymous inner class
  • As "regular" inner classes method-local inner class couldn't declare static content
  • As the method-local inner class could use only final local variables, so it is just method-local inner class
  • As "regular" inner class has access to all outer class members
  • Anonymous inner class allows you to create the interface implementer
  • You can pass the anonymous inner class as the method parameter
Static nested classes
  • The static nested class has the access only to the static members of the outer class
  • Could declare the own static members
  • You do not need the instance of the outer class, actually even when you have you couldn't create the instance of the static inner with the instance of the outer class. The only way to create instance of staitc nested class looks like: Outer.Inner inner = new Outer.Inner();




No comments:

Post a Comment