Javatpoint Logo
Javatpoint Logo

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.

Inner 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.

Outer Class

The class in which nested class is defined is called outer class.

Nested Class

Java allows us to define a class within a class that is known as a nested class. It may be static or non-static. The major difference between static and non-static class is that:

  • An instance of the static nested class can be created without creating an instance of its outer class.
  • The static and non-static members of an outer class can be accessed by an inner class.
  • The static members of the outer class can be accessed only by the static class.

Remember

  • All static classes are nested classes but vice-versa is not true.
  • It can access only static members of the outer class.
  • Non-static variable and instance methods cannot be accessed within the static class. If you try to access a non-static reference from a static field, it throws an error: Cannot make a static reference to the non-static field.
  • We can create static blocks, variables, and methods inside a static class.
  • A class may have multiple static classes.
  • We cannot access the static class if it is inside the static block.
  • There may be any number of static classes within a static class.

Java Static Class Example

JavaStaticClassExample.java

Output:

Javatpoint

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.


Next TopicWhat is Core Java




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA