A class can also be declared as a member of another class, just like instance variables and instance methods. So, when a class is defined in another class then it is known as a nested class in Java. A nested class never exists independently without an outer class. Therefore, its scope limits within the outer class only.
Point to remember:-
There are broadly two types of nested classes:
1) Static
2) Inner class
Both categories are distinguished from one another in a way that static class doesn’t require an object of the outer class in order to instantiate itself, whereas inner class requires an instance of the outer class in order to instantiate itself.
As explained above, it can be instantiated without the help of instance of the outer class. It is preceded by the static keyword. It is not possible to access non-static members of the outer class directly. Therefore, it is not frequently used. [ Read about static keyword and its uses ]
Syntax:
class A { static class B { } }
Program:
public class Test { static int y; //Static variable Test () // constructor { System.out.println ("Hi!I am constructor"); } static class nestedClass{ void display () { System.out.println ("I am from static nested class"); } } public static void main (String[]args) { Test obj = new Test (); Test.nestedClass o = new Test.nestedClass (); o.display (); } }
Output:
Hi!I am constructor
I am from static nested class
These are similar to the data member and instance method of a class that requires an object or instance of enclosing class in order to instantiate themselves. It can access static and non-static members of the outer class directly.
Syntax:
class A{ class B{ } }
Program:
public class OuterClass { OuterClass () { InnerClass obj = new InnerClass (); obj.display (); } void show () { System.out.println ("Hi! I am outer class."); } class InnerClass { void display () { System.out.println ("Hi! I am inner class."); } } public static void main (String[]args) { OuterClass obj = new OuterClass (); obj.show (); } }
Output:
Hi! I am inner class. Hi! I am outer class.
A local class is an inner class that is defined within the block or method similar to local variables. It can’t be accessed outside a method or block. It is instantiated with the method or block in which it is defined.
Syntax:
class A { returnType methodName () { class B { } } }
Program:
public class OuterClass { void show () { // local inner class class localInnerClass { void display () { System.out.println ("Hi! I am local inner class."); } } localInnerClass obj = new localInnerClass (); obj.display (); } public static void main (String[]args) { OuterClass obj = new OuterClass (); obj.show (); } }
Output:
Hi! I am local inner class.
An anonymous inner class is a class without having any name. For example, suppose there is a class A having show() method and I want to change the behavior of show method for a specific type of object.
In order to achieve this, we need to create another class B inheriting class A and override the method show.
Another way is Annonymous class, in this, we don’t need to create another class, instead, we define the class body in between curly braces (just like a simple block) without the name of the class for a specific object and override that method that we want. The main purpose of the anonymous inner class is to create concise code.
Its implementation is provided just after the object creation statement but before the semicolon. It proves handy when it comes to working with event handling.
Point to remember:-
Syntax:
className obj = new className{ //body }
Program:
class SomeClass { void display () { System.out.println ("Hi! I am real one."); } } public class OuterClass { public static void main (String[]args) { SomeClass obj = new SomeClass (){ void display (){ System.out.println ("Hi! I am anonymous inner class.");} }; obj.display ();} }
Output:
Hi! I am anonymous inner class.
This post was last modified on December 5, 2020