Static Keyword in Java

Introduction 

 
In Java, we have a keyword called static that is mainly used for memory management. There are 57 keywords in Java, but this static keyword is a special one that can be used in four different applications. In this article, I will give a brief tutorial on the static keyword.
 
A static keyword can be used with blocks, variables, methods, and classes. As we know, in object-oriented programming everything is based upon objects. If you want to access the variable or method, you need to create an object. With the help of the object, we can access the variable/method. However, if we declare a static member, it provides a feature so we can access these without creating the object or object reference.
 

Static Block

 
We have a block called static that's executed when the class loaded into the JVM. It's executed only once and is usually used to initialize the variables. In Java, if we want to print even a hello world, we have to write the code within the static main function or you can access the method through an object. If we want to do something without disturbing objects, then we can do that process with the help of a static block. The main purpose of a static block is to initialize the static variable, as we have shown in the example. On the other hand, constructors are used to initialize local variables. (Note: we cannot initialize a non-static (instance) variable inside the static block.)
 
 
Fig 1 Snippet of static block demo
 
Fig 2 Output of static block demo
 

Static Variable

 
If we declare a variable as static, then that variable does not relate to any objects. That variable value will be shared among all the objects. We access the local variable via the help of objects, but in the case of static variables, we can call them through the class name, since it's common to all the objects. The object will create a memory in heap, but the static variable will not be in the heap. Instead, it will be in the class loader memory.
 
Fig 3: Understanding of Static variable
 
From the understanding of static variables, we learned that if there is a common thing between objects, it can be shared through a static variable. If we use static variable it calls once irrespective of the number of objects. In the case of constructors, the number of times execution of the constructors will be based on the number of objects. So this is why we call the static variable, it is better for our project with respect to memory management.
 
Here's a demo to explain the static variable with Java code. Consider we are creating a project with employee details and the fields are as follows, employee ID, salary, and CEO of the company. Here, the first two fields are a variable, whereas the CEO of the company will be the same for all the employees. In this case, it’s constant, so we can declare CEO as static.
 
Fig 4: Snippet for understanding static variable
 
 
Fig 5: Output
 

Static Function

 
A function that needs to be accessed without an object has to be declared as a static function. In Java, we have the main function that is always declared as static. Because that is the first function, it has to be executed, even before creating an object. In a static function, we cannot access nonstatic variables. In the same way, they can call other static functions only directly.
 

Static Class

 
In java, the static keyword can be used for creating a static class as well. But only the inner class can be created using a static keyword. If it’s a static inner class, it does not need a reference of the outer class. A static class cannot access the non-static methods and variables of the outer class. Let’s see an example of a static class.
 
 
Fig 6: Static Class Demo
 
 
Fig 7: Output
 

Conclusion

 
In this article, we have discussed the various purposes of a static keyword in Java. Let’s use the static keyword in our project whenever it's required since it helps with memory management. I hope this article helped give some clarity to the static keyword. Thanks for your valuable time!


Similar Articles