Introduction
An inner class in Java is a class declared inside another class or within a block of code. Inner classes help group related classes, improve encapsulation, and keep supporting code close to where it is used.
The broader term is nested class. A nested class can be static or non-static.
Types of Inner Classes in Java
Java provides four main types of nested and inner classes:
Static nested classes
Non-static inner classes
Local inner classes
Anonymous inner classes
1. Static Nested Class
A static nested class is declared with the static keyword inside another class.
It belongs to the outer class instead of an outer-class object. Therefore, it can be created without creating an object of the outer class.
A static nested class can directly access only the static members of its outer class.
Static Nested Class Example
// Outer classclass Outer { // Static member of the outer class static String message = "Hello from static nested class."; // Static nested class static class Nested { // Method of the static nested class void display() { // Access the static member of the outer class System.out.println(message); } }}public class Main { public static void main(String[] args) { // Create the nested-class object without an Outer object Outer.Nested object = new Outer.Nested(); // Call the nested-class method object.display(); // Output :- Hello from static nested class. }}In this example, the Nested class is associated with the Outer class. An object of Outer is not required to create it.
Static nested class
2. Non-Static Inner Class
A non-static inner class is declared inside another class without using the static keyword.
It belongs to an object of the outer class. Therefore, an outer-class object is required before its object can be created.
A non-static inner class can directly access static and non-static members of its outer class, including private members.
Non-Static Inner Class Example
// Outer classclass Outer { // Private instance member of the outer class private String message = "Hello from non-static inner class."; // Non-static inner class class Inner { // Method of the inner class void display() { // Access the private member of the outer class System.out.println(message); } }}public class Main { public static void main(String[] args) { // Create an object of the outer class Outer outer = new Outer(); // Create the inner-class object using the outer object Outer.Inner inner = outer.new Inner(); // Call the inner-class method inner.display(); // Output :- Hello from non-static inner class. }}In this example, the Inner object is connected to the outer object. It can directly access the private message attribute of Outer.
Non static inner class
3. Local Inner Class
A local inner class is declared inside a method, constructor, loop, or another block of code.
It can only be created and used inside the block where it is declared. A local inner class can access members of the outer class and final or effectively final local variables.
Local Inner Class Example
// Outer classclass MessagePrinter { // Method containing a local inner class void printMessage() { // This variable is effectively final String message = "Hello from local inner class."; // Local inner class declared inside the method class LocalPrinter { // Method of the local inner class void display() { // Access the local variable of the enclosing method System.out.println(message); } } // Create the local-class object LocalPrinter printer = new LocalPrinter(); // Call the local inner-class method printer.display(); // Output :- Hello from local inner class. }}public class Main { public static void main(String[] args) { // Create an object of the outer class MessagePrinter printer = new MessagePrinter(); // Call the method containing the local inner class printer.printMessage(); }}The LocalPrinter class is available only inside printMessage(). Code outside this method cannot create its object directly.
Local inner class
4. Anonymous Inner Class
An anonymous inner class is a class without a name. It is declared and instantiated in a single expression.
Anonymous inner classes are commonly used when a class or interface implementation is required only once.
Anonymous Inner Class Example
// Interface containing one required methodinterface Greeting { // Method to be implemented void sayHello();}public class Main { public static void main(String[] args) { // Create an anonymous class that implements Greeting Greeting greeting = new Greeting() { // Provide the interface method implementation @Override public void sayHello() { System.out.println("Hello from anonymous inner class."); } }; // Call the method implemented by the anonymous class greeting.sayHello(); // Output :- Hello from anonymous inner class. }}The anonymous class implements the Greeting interface without requiring a separate named class.
Anonymous inner class
Difference Between Types of Inner Classes
Type | Declared Where? | Requires Outer Object? | Has a Name? |
|---|---|---|---|
Static nested class | Inside another class with | No | Yes |
Non-static inner class | Inside another class without | Yes | Yes |
Local inner class | Inside a method or block | Depends on its context | Yes |
Anonymous inner class | During object creation | Depends on its context | No |
Advantages of Inner Classes
Group closely related classes together
Improve encapsulation
Hide helper classes from unrelated code
Make code easier to organize
Allow access to outer-class members
Provide one-time implementations using anonymous classes
Summary
Java nested and inner classes allow one class to be declared inside another class or block.
Static nested classes do not require an outer object. Non-static inner classes belong to an outer object. Local inner classes are limited to a block, while anonymous inner classes provide short, one-time implementations without a class name.
Be the first to add a comment.