-
Nested class - defining a class inside another class
class OuterClass { ... class NestedClass { ... } }- Terminology: Divided into two categories
- static:
staticnested classes - non-static: non-static nested classes (inner classes)
- static:
- Terminology: Divided into two categories
-
Nested class is a member of it's enclosing class
-
Non-static nested classes (inner classes) have access to other members of enclosing class (even if declared private)
-
Static nested classes do not have access to other members of enclosing class
- It can be declared:
private,public,protected, package private (default)- Outer classes can be only:
publicor package private (default)
- Outer classes can be only:
- It can be declared:
- A way of logically grouping classes that are only used in one place
- If a class is used in only one other class, then they can be grouped and kept together
- The package can be more streamlined (?)
- Increases encapsulation:
- If A and B are two top level classes, where B needs access to members of A (which would be private):
- If we hide class B within class A, A's members can be declared private and B can access them
- B itself can be hidden from outside world (inner details)
- If A and B are two top level classes, where B needs access to members of A (which would be private):
- It can lead to more readable and maintainable code
- Nesting small classes within top-level classes places the code closer to where it is used
-
It is associated with it's outer class
-
It cannot refer directly to:
- Instance variables of enclosing class
- Instance methods of enclosing class
-
The instance members can only be used through object reference
-
Note: static nested class interacts with instance members of its outer class (and other classes) just like any other top level class
- It is behaviorally a top-level class that is nested in another top-level class (for packaging convenience)
-
They are accessed using enclosing class name:
OuterClass.StaticNestedClass-
To instantiate an object of static nested class:
OuterClass.StaticNestedClass outerObject = new OuterClass.StaticNestedClass();
-
-
It is like instance methods and variables
- It is associated with an instance of enclosing class and has direct access to other object's methods and fields
- It cannot define any static members itself - because it is associated with an instance
-
Inner class objects can only reside instances of outer class
class OuterClass { ... class InnerClass { ... } }-
Instance of
InnerClasscan exist only within instance ofOuterClass- It has direct access to methods and fields of its enclosing instance
-
First instantiate outer class and then instantiate inner class (within outer object)
OuterClass outerObject = new OuterClass(); OuterClass.InnerClass innerObject = outerObject.new InnerClass();- Special kinds of inner classes:
-
-
If a declaration of a type (member variable or parameter name) in a scope (inner class or method definition) has same name as another declartion in enclosing scope, then declaration shadows declaration of enclosing scope
- Shadowed declaration cannot be accessed by name alone
-
Example:
public class ShadowTest { public int x = 0; class FirstLevel { public int x = 1; void methodInFirstLevel(int x) { System.out.println("x = " + x); System.out.println("this.x = " + this.x); System.out.println("ShadowTest.this.x = " + ShadowTest.this.x); } } public static void main(String[] args) { ShadowTest st = new ShadowTest(); ShadowTest.FirstLevel fl = st.new FirstLevel(); fl.methodInFirstLevel(23); } }-
Output:
x = 23 this.x = 1 ShadowTest.this.x = 0 -
x (parameter) shadows variable of inner class FirstLevel
-
thisneeds to be used to access member variable -
<outer-classname>.thisis used to access outerclass member variable
-