A brief analysis of Static Class and Static inner Class and non static inner Class in Java

  • 2020-04-01 04:23:53
  • OfStack

Can a class in Java be static? The answer is yes. In Java we can have static instance variables, static methods, and static blocks. Classes can also be static.

Java allows us to define static classes within a class. Such as nested class. Classes that are enclosed in nested classes are called outer classes. In Java, we cannot make a top level class static. Only inner classes can be static.

        What's the difference between a static inner class and a non-static inner class? Here are the main differences.

      (1) internal static classes do not need references to external classes. But a non-static inner class needs to hold a reference to the outer class.

      (2) the non-static inner class can access the static and non-static members of the external class. A static class cannot access non-static members of an external class. He can only access static members of external classes.

      (3) a non-static inner class cannot be created without the external class entity. A non-static inner class can access the data and methods of the external class because it is inside the external class.

Based on the discussion above, we can use these features to make programming easier and more efficient.



class OuterClass{
  private static String msg = "GeeksForGeeks";
  //Static inner class
  public static class NestedStaticClass{
    //Static inner class Only static members of external classes can be accessed 
    public void printMessage() {
     //Try changing MSG to non-static, which will cause a compilation error
     System.out.println("Message from nested static class: " + msg); 
    }
  }
  //Non-static inner class
  public class InnerClass{
    //Both static and non-static methods can be accessed in non-static inner classes
    public void display(){
     System.out.println("Message from non-static nested class: "+ msg);
    }
  }
} 
class Main
{
  //How do I create instances of static and non-static inner classes
  public static void main(String args[]){
    //Creates an instance of a static inner class
    OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass();
    //Creates a non-static method for a static inner class
    printer.printMessage();  
    //To create a non-static inner class, we need an instance of the outer class
    OuterClass outer = new OuterClass();    
    OuterClass.InnerClass inner = outer.new InnerClass();
    //Calls a non-static method of a non-static inner class
    inner.display();
    //We can also combine the above steps to create an inner class instance in one step
    OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();
    //Again, we can now call the inner class method
    innerObject.display();
  }
}

The above content is this site to introduce you to Java Static Class related information, hope to help you learn Java Static Class.


Related articles: