In Java, you can define a class within another class. Such class is known as nested class.
Ex:
1 2 3 4 5 6 | class OuterClass { // ... class NestedClass { // ... } } |
There are basically four types of inner classes in java.
- Nested Inner Class
- Method Local Inner Classes
- Static Nested Classes
- Anonymous Inner Classes
Non-Static Nested Class (Inner Class)
A non-static nested class is a class within another class. It has access to members of the enclosing class (outer class). It is commonly known as inner class
.
Since the inner class
exists within the outer class, you must instantiate the outer class first, in order to instantiate the inner class.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | // Java Program to Demonstrate Nested class // Class 1 // Helper classes class Outer { // Class 2 // Simple nested inner class class Inner { // show() method of inner class public void show() { // Print statement System.out.println("In a nested class method"); } } } // Class 2 // Main class class Main { // Main driver method public static void main(String[] args) { // Note how inner class object is created inside // main() Outer.Inner in = new Outer().new Inner(); // Calling show() method over above object created in.show(); } } |
Output:
1 | In a nested class method |
.
) operator to create an instance of the inner class using the outer class.Accessing Members of Outer Class
We can access the members of the outer class by using this keyword.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | class Car { String carName; String carType; // assign values using constructor public Car(String name, String type) { this.carName = name; this.carType = type; } // private method private String getCarName() { return this.carName; } // inner class class Engine { String engineType; void setEngine() { // Accessing the carType property of Car if(Car.this.carType.equals("4WD")){ // Invoking method getCarName() of Car if(Car.this.getCarName().equals("Crysler")) { this.engineType = "Smaller"; } else { this.engineType = "Bigger"; } }else{ this.engineType = "Bigger"; } } String getEngineType(){ return this.engineType; } } } public class Main { public static void main(String[] args) { // create an object of the outer class Car Car car1 = new Car("Mazda", "8WD"); // create an object of inner class using the outer class Car.Engine engine = car1.new Engine(); engine.setEngine(); System.out.println("Engine Type for 8WD= " + engine.getEngineType()); Car car2 = new Car("Crysler", "4WD"); Car.Engine c2engine = car2.new Engine(); c2engine.setEngine(); System.out.println("Engine Type for 4WD = " + c2engine.getEngineType()); } } |
Output:
1 2 | Engine Type for 8WD= Bigger Engine Type for 4WD = Smaller |
Method Local Inner Classes
Inner class can be declared within a method of an outer class which we will be illustrating in the below example where Inner is an inner class in outerMethod().
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | // Java Program to Illustrate Inner class can be // declared within a method of outer class // Class 1 // Outer class class Outer { // Method inside outer class void outerMethod() { // Print statement System.out.println("inside outerMethod"); // Class 2 // Inner class // It is local to outerMethod() class Inner { // Method defined inside inner class void innerMethod() { // Print statement whenever inner class is // called System.out.println("inside innerMethod"); } } // Creating object of inner class Inner y = new Inner(); // Calling over method defined inside it y.innerMethod(); } } // Class 3 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating object of outer class inside main() // method Outer x = new Outer(); // Calling over the same method // as we did for inner class above x.outerMethod(); } } |
Output:
1 2 | inside outerMethod inside innerMethod |
Static Nested Class
Static nested classes are not technically inner classes. They are like a static member of outer class.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | // Java Program to Illustrate Static Nested Classes // Importing required classes import java.util.*; // Class 1 // Outer class class Outer { // Method private static void outerMethod() { // Print statement System.out.println("inside outerMethod"); } // Class 2 // Static inner class static class Inner { public static void display() { // Print statement System.out.println("inside inner class Method"); // Calling method inside main() method outerMethod(); } } } // Class 3 // Main class class GFG { // Main driver method public static void main(String args[]) { // Calling method static display method rather than an instance of that class. Outer.Inner.display(); } } |
Output:
1 2 | inside inner class Method inside outerMethod |
Accessing members of Outer class
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class MotherBoard { String model; public MotherBoard(String model) { this.model = model; } // static nested class static class USB{ int usb2 = 2; int usb3 = 1; int getTotalPorts(){ // accessing the variable model of the outer classs if(MotherBoard.this.model.equals("MSI")) { return 4; } else { return usb2 + usb3; } } } } public class Main { public static void main(String[] args) { // create an object of the static nested class MotherBoard.USB usb = new MotherBoard.USB(); System.out.println("Total Ports = " + usb.getTotalPorts()); } } |
Anonymous Inner Classes
Anonymous inner classes are declared without any name at all. They are created in two ways.
- As a subclass of the specified type
- As an implementer of the specified interface
As a subclass of the specified type
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | // Java Program to Illustrate Anonymous Inner classes // Declaration Without any Name // As a subclass of the specified type // Importing required classes import java.util.*; // Class 1 // Helper class class Demo { // Method of helper class void show() { // Print statement System.out.println( "i am in show method of super class"); } } // Class 2 // Main class class Flavor1Demo { // An anonymous class with Demo as base class static Demo d = new Demo() { // Method 1 // show() method void show() { // Calling method show() via super keyword // which refers to parent class super.show(); // Print statement System.out.println("i am in Flavor1Demo class"); } }; // Method 2 // Main driver method public static void main(String[] args) { // Calling show() method inside main() method d.show(); } } |
Output:
1 2 | i am in show method of super class i am in Flavor1Demo class |
As an implementer of the specified interface
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | // Java Program to Illustrate Anonymous Inner Classes // Declaration Without Any Name // As an implementer of Specified interface // Interface interface Hello { // Method defined inside interface void show(); } // Main class class GFG { // Class implementing interface static Hello h = new Hello() { // Method 1 // show() method inside main class public void show() { // Print statement System.out.println("i am in anonymous class"); } }; // Method 2 // Main driver method public static void main(String[] args) { // Calling show() method inside main() method h.show(); } } |
Output:
1 | i am in anonymous class |