Java Program to Illustrates Use of Static Inner Class


Here, we will demonstrate the usage of static inner class using the Java program.

Before diving deep into the topic, let us get acquainted with the term Static Inner Class .

Static Inner Class

An inner class is the one, which is defined within another class.

A Static Inner Class is a nested class that is a static member of the outer class.

Other static members can be used to access it without first instantiating the outer class.

A static nested class lacks access to the instance variables and methods of the outer class, much like static member’s java.

Example 1

In the following program, the ability of a static inner class to access static members of the outer class is illustrated, without the need of an instance of the outer class to be created.

// Java Program to demonstrate the Usage of Static Inner Class
// Beginning of Outer class
public class Tutorials_point1 {  
    // Display message of inner class
    static String s = "Have a great year ";
    // Beginning of Static Inner Class
    static class InnerClass {
        // Static Inner Class Method
        public void show(){
            // Show the message of inner class
            System.out.println("Happy New Year " + s);
        }
    }
    // Beginning of the Main method
    public static void main(String[] args){
        // Creation of an instance of the static inner class
        InnerClass instance = new InnerClass();
        // invoking the show() method
        // by the instance variable of inner class
        instance.show();
    }
}

Output

Happy New Year Have a great year 

In the above program, an outer class named "Tutorials_point1" is defined which contains a static string variable "s". It also contains a static inner class named "InnerClass" which has a method called "show()" that displays a message by means of the static string variable from the outer class.

In the main method, an instance of the static inner class is created and the "show()" method is invoked on that instance, which displays the message "Happy New Year" followed by the value of the static string variable "s".

Example 2

In the following program, the ability to create an instance of a static inner class is illustrated without the need of an instance of the outer class to be created first. The syntax for creating an instance of a static inner class is different from that of creating an instance of a non-static inner class.

// Java Program to demonstrate the Usage of Static Inner Class
// Beginning of Outer class
public class Tutorials_point2 {  
    // Static string message
    static String s = "Have a great year ";  
    // Beginning of Static Inner Class
    static class InnerClass {  
        // Static Inner Class Method
        public void show() {
            // Display message in inner class
            System.out.println("Happy New Year " + s);
        }
    }  
    //beginning of Main method
    public static void main(String[] args) {
        // Creation of an instance of the outer class
        Tutorials_point2.InnerClass instance = new Tutorials_point2.InnerClass();  
        // invoking method of static inner class via
        // the instance variable of outer class
        instance.show();
    }
} 

Output

Happy New Year Have a great year 

In the program, an outer class named "Tutorials_point2" containing a static string variable "s" is created. It also contains a static inner class called "InnerClass" which has a method named "show()" that displays a message using the static string variable from the outer class.

In the main method, an instance of the static inner class is created. The "show()" method is then invoked on that instance, which displays the message "Happy New Year" followed by the value of the static string variable "s".

This article threw light on how to use the Static inner class in Java. The article began with the discussion of the term Static inner class. Further, two implementations are shown to get the clearer picture of the topic.

Updated on: 11-Aug-2023

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements