Static Nested Class

Sometimes we can declare an inner class with static modifiers such types of inner classes are known as static nested classes.

In the case of the normal inner class, the inner class object is strongly associated with the outer class object but in the case of a static nested class without the existence of the outer class object there may be a chance of the existence of nested class object hence static nested class object is not strongly associated with outer class object.

public class Outer {
    static class Nested{
        public void method(){
            System.out.println("Static nested class method");
        }
    }

    public static void main(String[] args) {
        Nested object = new Nested();
        object.method();
    }
}
Static Nested Class

If we want to create a nested class object outside the outer class then we can create as follows-

Outer.Nested object = new Outer.Nested();

In a normal inner class, we can not declare static members but in a static nested class we can declare static members including the main method, hence we can run the static nested class directly from the command prompt.

public class Outer {
    static class Nested {

    public static void main(String[] args) {
        System.out.println("Static nested class main method");
    }
}
    public static void main(String[] args) {
        System.out.println("Outer class main method");
    }
}

From a normal inner class, we can access both static and nonstatic members of the outer class directly but in a static nested class, we can only access static members of the outer class directly. we can not access nonstatic members.

public class Outer {
    int i =23;
    static int j= 32;
    static class Nested{
        public void method(){
            System.out.println(j);
            System.out.println(i);

   }
    }
    public static void main(String[] args) {
      Nested object = new Nested();
      object.method();
    }
}
non-static variable i cannot be referenced from a static context

Difference between Normal Inner Class and Static Nested Class

Normal Inner Class Static Nested Class
Without the existence of outer class object there is no chance of inner class object existence that is an inner class object is strongly associated with the outer classWithout existence of outer class object, there may be a chance of inner class object existence that is Static Nested Class object is not strongly associated with the outer class
In normal inner classes, we can not declare static members In static nested classes we can declare static members
In normal inner classes we can not declare main methodIn static nested classes we can declare main method
From normal inner classes we can access both static and non static members of outer class directlyFrom static nested classes, we can only access static members of outer class directly.

Combination of Nested Classes and Interfaces

Class Inside a Class

If there is no chance of the existence of an object without the existance of another type of object then we can declare a class inside a class.

Example- Without the existence of a vehicle there is no chance of the existence of a car. hence we can declare car class inside vehicle class.

Interface Inside a Class

Inside a class, if multiple implementation of an interface is required and all these implementations are related to a particular class then we can define an interface inside a class.

public class VehicleType {
    interface Vehicle {
        public int NoOfVehicle();
        }
        class Bicycle implements Vehicle{

            @Override
            public int NoOfVehicle() {
                return 2;
            }
        }
        class Car implements Vehicle{

            @Override
            public int NoOfVehicle() {
                return 4;
            }
        }
    }

Interface Inside Interface

We can declare the interface inside the interface. For Example- A map is a group of key-value pair and each key-value pair is an entity. If there is no map object then there is no chance of entity object. Hence interface entity is defined inside the map entity.

Every interface present inside the interface is always public and static whether we are declaring or not. Hence we can implement the inner interface directly without implementing an outer interface, similarly, whenever we are implementing the outer interface it is not compulsory to implement an inner interface that is we can implement the outer and inner interface independently.


    interface OuterInterface {
    public void method1();
    interface InnerIntercae{
        public void method2();
    }
    }
    class Impl1 implements OuterInterface{

        @Override
        public void method1() {
            System.out.println("Outer interface method override");
        }
    }
    class Impl2 implements OuterInterface.InnerIntercae{

        @Override
        public void method2() {
            System.out.println("Inner interface method override");
        }
    }
    class RunDemo {
        public static void main(String[] args) {
            Impl1 object = new Impl1();
            object.method1();
            Impl2 object1 = new Impl2();
            object1.method2();
        }
    }
Interface Inside Interface

Class Inside Interface

If the functionality of a class is closely associated with the interface then it is highly recommended to declare a class inside the interface.


    interface EmailService{
    public void sendmail(Email e);
    
    class Email{
        String sender;
        String receiver;
        String subject;
        String body;
        
    }
        
    }

We can also implement a class inside interface to provide a default implementation for that interface.

Note- The class which is declared inside the interface is always public static.

Similar Java Tutorials

Leave a Comment