Java Static Class

www.igi‮editf‬a.com

In Java, a static class is a class that is declared with the "static" modifier. However, it is important to note that the term "static class" is not a part of the Java language; rather, it is a term that is sometimes used to describe certain types of classes.

There are two main types of static classes in Java: static nested classes and static inner classes. Both types of classes are defined within another class and have the "static" modifier, but they have different properties.

A static nested class is a static class that is defined at the same level as other static members of the enclosing class. It can only access static members of the enclosing class, and it does not have access to the instance variables of the enclosing class. Here is an example of a static nested class:

public class OuterClass {
    static class NestedClass {
        // code here
    }
}

A static inner class, on the other hand, is a static class that is defined inside an instance of the enclosing class. It can access both static and non-static members of the enclosing class. Here is an example of a static inner class:

public class OuterClass {
    private static int staticVar = 0;

    static class InnerClass {
        void printStaticVar() {
            System.out.println("Static variable: " + staticVar);
        }
    }
}

In this example, the InnerClass is a static inner class that has access to the static variable staticVar of the enclosing class.

In summary, while the term "static class" is not a formal concept in Java, static nested classes and static inner classes are two types of classes that have the "static" modifier and are defined within another class. They have different properties and can be used in different ways to improve encapsulation and code organization.