Java allows us to define classes inside other classes. Nested classes enable us to logically group classes that are only used in one place, write more readable and maintainable code & increase encapsulation.
Several types of nested classes available in language:-
The scope of nested classes is bounded by the scope of its enclosing class. Nested class doesn't exist independently of class outer class.
A Nested class has access to members, including private members of class in which it is nested. However, reverse is not true i.e, the enclosing class does not have access to members of nested class.
A Nested class is also a member of its enclosing class, a nested class can be declared private, public, protected or package private (default).
You use nested class to reflect and to enforce relationship between two classes. you should define a class within another class when the nesred class makes sense only in the context of its enclosing class or when it relies on enclosing class of its function.
For Example:- A text cursor might make sense only in context of a text component.
As a member of enclosing class, it has unlimited access to its enclosing class's members, even if they all declared private. It is fully consistent with the meaning of private and other
access specifiers. The Access Specifiers restrict access to members for classes outside enclosing class. Nested class is inside its enclosing class so that it has access to enclosing class's members.
As with instance methods & variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's instance variables and methods. Also, because an inner class is associatd with an instance. it cannot define any static members itself.
The term nested class reflects the syantactic relationship between two classes. i.e. the code for one class appears within the code of another.
The term inner class reflects relationship between objects are instances of two classes.The instance of inner class can exist only within an instance of enclosing class and has direct access to instance variables and methods of its enclosing instance.
Member functions of a nested class follow regular access rules and have no special access privilages to members of their enclosing classes. Member functions of enclosing class have no special access to members of nested class.
You can define member functions and static data members of nsted class in namespace.
It is a way of logically grouping classes that are only used in one place. If a class is useful to only one other class, then it is logically to embed it in that class and keep two together.
Nesting such "helper classes" makes pacxkage more .......?.
Increase Encapsulation:- consider two top level classes A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A. A's member can be declared private and B can access them.
If a nested class, has static modifier applied in it, then it is called static nested class. As due to this, static nested class can access only static members of its outer class. i.e., it cannot
refer to non-static member of its enclosing class directly. It can use them only through an object refrence. Due to this restriction, static nested class is rarely used.
Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
to create an object for static nested class.
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
class OuterClass{ static int outerx = 10; int outery = 20; private static int outerprivate = 30; static class staticNestedClass { void display() { System.out.println("Outer x="+outerx); System.out.println("Outer private= "+outerprivate); } } } public class staticNestedClassDemo { public static void main(String args[]) { OuterClass.staticNestedClass nestedObject= new OuterClass.staticNestedClass(); nestedObject.display(); } }
Output: Outer x=10 Outer private= 30
class Outer{ public void display() { Inner in = new Inner(); in.show(); } class Inner { public void show() { System.out.println("Inner Class"); } } } public Test { public static void main(String args[]) { Outer ot= new Outer(); ot.display(); } }
Output: Inner Class
This is a special type of inner class, in which the class in defined inside a method or scope block.
public class BCE{ void Bti() { class Local { void run() { System.out.println("Inner run Method"); } } Local local = new Local(); local.run(); } public static void main(String args[]) { BCE bce = new BCE(); bce.Bti(); } }
Output: Inner run Method
Can be used to define an implementation of an iterface or an abstract class without having to create a resuable implemetation. Key Points:-
interface inter { void type(); } class chk { public static void main(String args[]) { inter an = new inter() { public void type() { System.out.println("Anonymous Class"); } }; an.type(); } }
Output: Anonymous Class