Open In App

Difference Between Static and Non Static Nested Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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

 

Java




// 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:

 

  • The static inner class can access the static members of the outer class directly. But, to access the instance members of the outer class you need to instantiate the outer class.
  • Nested static class doesn’t need a reference of Outer class but a nonstatic nested class or Inner class requires Outer class reference.
  • A non-static nested class has full access to the members of the class within which it is nested. A static nested class does not have a reference to a nesting instance, so a static nested class cannot invoke non-static methods or access non-static fields of an instance of the class within which it is nested.
  • Another difference between static and non-static nested class is that you can not access non-static members e.g. method and field into nested static class directly. If you do you will get errors like “nonstatic member can not be used in the static context”. While the Inner class can access both static and non-static members of the Outer class.

 



Last Updated : 19 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads