HomeCore JavaJava static nested class with examples

Java static nested class with examples

1. java static nested class

Nested classes that are declared with static keyword is called as static nested class. With in java static nested class you can only access the static members of the outer class directly.

1.1. Syntax of java static nested class

//outer class
class OuterClass {
   ...

     //inner class
     static class StaticNestedClass {

           ...
       }
}
  • Inside static nested classes we can declare static members including main() method also.
  • From the normal inner class we can access both static and non static members of outer class but from static nested class we can access only static members of outer class.

1.2. Example 1

public class StaticNestedClassDemo {
 
    static private final int regestrationId = 1001;
    int grade = 9;
    
    static class Student {
        int studentId = 101;
 
        public void displaySchoolInfo() {
            System.out.println("Instance method of static nested class");
            System.out.println("Regestration Id : " + regestrationId);
            // System.out.println(grade); // we can't access the non static data of outer class
        }
 
        public static void showStudentInfo() {
            System.out.println("Static method of static nested class");
        }
 
    }
 
    public static void main(String[] args) {
        StaticNestedClassDemo.Student obj = new StaticNestedClassDemo.Student();
        obj.displaySchoolInfo(); // Accessing instance method
 
        StaticNestedClassDemo.Student.showStudentInfo(); //outerclass.innerclass.staticMethod()
    }
}

In the above program, we are accessing the static nested class code without creating an object for the outer class. We can also take the main() method inside the static nested class. Run above program, following is the output.

Output :

Instance method of static nested class
Regestration Id : 1001
Static method of static nested class

1.2. Example 2

We can define static nested interface also within class.

//outer class
class OuterClass {
   ...

     //inner class
     static interface StaticNestedInterface {

           ...
       }
}
public class SNCMainMethodDemo {
	 
	static private final int regestrationId = 1001;
	static private final String name = "Peter";
	
	   static interface IStudent{
		   public String appendRank();
	   }
	
       static class Student implements IStudent{
    	   
    	   static private final int regestrationId = 1002;
    	   private static final int rank = 33;
    	   
    	   @Override
	   		public String appendRank() {
	   			
	   			return name+" "+rank;
	   		}
    	   
    	   public static void main(String[] args) {
			
    		   System.out.println("Student name : "+name);
    		   System.out.println("Static Nested class Registration Id "+regestrationId);
    		   System.out.println("Outer class Registration Id "+SNCMainMethodDemo.regestrationId);
    		   printName();
    		   new SNCMainMethodDemo().printNameRank();
		   }
       } // End of nested class
       
       public static void printName() {
    	   System.out.println("name : "+name);
       }
       
       public void printNameRank() {
    	   SNCMainMethodDemo.IStudent student = new SNCMainMethodDemo.Student();
    	   System.out.println(student.appendRank());
       }
}

Run the SNCMainMethodDemo$Student class. Following is results of the program.

Output :

Student name : Peter
Static Nested class Registration Id 1002
Outer class Registration Id 1001
name : Peter
Peter 33

2. Inner classes vs static nested classes

Regular inner classesStatic nested classes
1. Inside normal or regular inner class we can’t declare static members.1. Inside static nested class we can declare static members.
2. Inside normal inner class we can’t declare main() method.2. Inside static nested class we can declare main() method.
3. From the normal or regular inner class we can access both static and non static members of outer class directly.3. From static nested class we can access only static members of outer class directly.

References

  1. Java documentation

LEAVE A REPLY

Please enter your comment!
Please enter your name here