Sunday, 29 January 2012
Do you like this Article?
The class which is defined within another class is called nested class.
It can be divided into 2 types. They are - static nested class & non-static nested class
- Nested class which is declared static is static nested class.
- Its methods & variables are associated with is mother/outer class.
- It can be used only through object reference.
- It is accessed using enclosing class name. Example: MotherClass.StaticNestedClass
- It interacts with enclosing class or other class like any other top-level class.
- It has access to the other members of the mother class, even they are private.
- It can't define any static member itself.
- Instance of inner class only valid within the instance of the enclosed class, & has direct access to the methods & fields of the enclosing instance.
- To instantiate innerclass, 1st we need to instantiate the mother class. Example:
MotherClass.InnerClass iObj = MotherObject.new InnerClass();
Types of Inner Class
1. Local Inner Class
2. Anonymous Inner Class
Local Inner Class:
· The inner class which is declared within the body of a method is known as local inner class.
· It is not a member the enclosing class, hence doesn’t have any access specifies.
· It has access to all the members of the enclosing class, even any final variable
within the scope.
Anonymous Inner Class:
· The inner class which is declared within the body of a method and has no name
is known as anonymous inner class.
· It is accessible only at the point of its declaration.