Open In App

Difference Between Static and Non Static Nested Class in Java

Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes.

A class can either be static or non-static in java. So there is a lot of difference between making a class static or non-static. There are two kinds of classes in Java, one is called a top-level class and the other is called a nested class. As the name suggested top-level class is a class that is declared in ‘.java’ file. 



On the other hand, a nested class is declared inside another class. The class which enclosed nested class is known as Outer class. In the Java programming language, you can not make a top-level class static. You can only make nested classes either static or non-static. If you make a nested class non-static then it also referred to as Inner class. 

User-cases:



Example 1: Static nested class




// Java Program to illustrate
// static nested class
 
// Importing input output classes
import java.io.*;
// Importing all classes from
// java.util package
import java.util.*;
 
// Class 1
// Helper class1
class ClassA {
 
    // Static variable
    static int x = 10;
 
    int y = 20;
 
    // Static variable with private
    // access modifier in ClassA
    private static int z = 30;
 
    // Class 2 - Nested static class
    // Helper Class 2
    static class ClassB {
 
        // Method of nested static class
        void get()
        {
            // Execution statements whenever
            // function is called
 
            // Print and display commands
            System.out.println("x: " + x);
            System.out.println("z: " + z);
        }
    }
}
 
// Class 3
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of static nested class
        // defined outside Main class
        ClassA.ClassB ob1 = new ClassA.ClassB();
 
        // Calling the method of static nested class
        // in main() method
        ob1.get();
    }
}

 
 

Output
x: 10
z: 30

 

Example 2: Non-static nested class

 




// Importing all input output classes
import java.io.*;
 
// Class 1
// Helper Class 1
class ClassA {
 
    // Static member variable of ClassA
    static int x = 10;
 
    int y = 20;
    public int z = 30;
 
    // Class 2 - Helper Class 2
    // Non-static nested class
    class ClassB {
 
        // Method of Non-static nested class
        void get()
        {
            // Execution command of get() method
 
            // Print and display commands
            System.out.println("x: " + x);
            System.out.println("y: " + y);
            System.out.println("z: " + z);
        }
    }
}
 
// Class 3
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of Class 1 in main() method
        ClassA ob1 = new ClassA();
 
        // Creating object of non-static nested class in
        // main()
        ClassA.ClassB ob2 = ob1.new ClassB();
 
        // Calling non-static nested class method
        // in main() method
        ob2.get();
    }
}

 
 

Output
x: 10
y: 20
z: 30

 

Now the difference between Static and Non-Static Nested Class in Java:

 

 


Article Tags :