Static class in Java

Can a class be static in Java?
The answer is Yes, some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block and Static Classes

Java allows a class to be defined within another class. These are called Nested Classes. The class in which the nested class is defined is known as the Outer Class. Unlike top level classes, Inner classes can be Static. Non-static nested classes are also known as Inner classes.

An instance of an inner class cannot be created without an instance of the outer class. Therefore, an inner class instance can access all of the members of its outer class, without using a reference to the outer class instance. For this reason inner classes can help make programs simple and concise.



What are the differences between static and non-static nested classes?
The following are major differences between static nested classes and inner classes.

  1. A static nested class may be instantiated without instantiating its outer class.
  2. Inner classes can access both static and non-static members of the outer class. A static class can access only the static members of the outer class.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate how to
// implement static and non-static
// classes in a Java program.
class OuterClass {
    private static String msg = "GeeksForGeeks";
  
    // Static nested class
    public static class NestedStaticClass {
  
        // Only static members of Outer class
        // is directly accessible in nested
        // static class
        public void printMessage()
        {
  
            // Try making 'message' a non-static
            // variable, there will be compiler error
            System.out.println(
                "Message from nested static class: "
                + msg);
        }
    }
  
    // Non-static nested class -
    // also called Inner class
    public class InnerClass {
  
        // Both static and non-static members
        // of Outer class are accessible in
        // this Inner class
        public void display()
        {
            System.out.println(
                "Message from non-static nested class: "
                + msg);
        }
    }
}
class Main {
    // How to create instance of static
    // and non static nested class?
    public static void main(String args[])
    {
  
        // Create instance of nested Static class
        OuterClass.NestedStaticClass printer
            = new OuterClass.NestedStaticClass();
  
        // Call non static method of nested
        // static class
        printer.printMessage();
  
        // In order to create instance of
        // Inner class we need an Outer class
        // instance. Let us create Outer class
        // instance for creating
        // non-static nested class
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner
            = outer.new InnerClass();
  
        // Calling non-static method of Inner class
        inner.display();
  
        // We can also combine above steps in one
        // step to create instance of Inner class
        OuterClass.InnerClass innerObject
            = new OuterClass().new InnerClass();
  
        // Similarly we can now call Inner class method
        innerObject.display();
    }
}

chevron_right


Output:



Message from nested static class: GeeksForGeeks
Message from non-static nested class: GeeksForGeeks
Message from non-static nested class: GeeksForGeeks

Reference Book: Introduction To Java Programming by Y. DANIEL LIANG

This article is contributed by Chandra Prakash. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Improved By : mailjesseduncan

Article Tags :
Practice Tags :


40


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.