Why We Use Static Class in Java?
In Java, static is a keyword that can be used with variables, classes, blocks, and methods. When we use the static keyword before any of them, it means that specified member belongs to a type itself. In other words, an instance of a static member is created and shared across all the instances of the class.
In this section, we will use static keyword with the Java class and also understand why we use a static class?
Java Static Class
We can declare a class static by using the static keyword. A class can be declared static only if it is a nested class. It does not require any reference of the outer class. The property of the static class is that it does not allows us to access the non-static members of the outer class.
To understand the concept of static class first we need to understand the concept of inner, outer, and nested class.
The classes that are non-static and nested are called inner classes. Note that we cannot create an instance of the inner class without creating an instance of the outer class. Without using the reference to the outer class instance, an instance of the inner class can access the members of its outer class. It makes the program simple and concise.
The class in which nested class is defined is called outer class.
Java Static Class Example
Why we Use static class in Java?
In Java, the static keyword is primarily used for memory management. We can use the static keyword with variables, methods, blocks, and classes. Using the static class is a way of grouping classes together. It is also used to access the primitive member of the enclosing class through the object reference. The static classes are loaded by the classloader at the time of the first usage only not when is enclosing class gets loaded.