Use of Static Keyword in Java
In Java, keywords are the reserved words that cannot be used as identifiers. One among them is
“Static“.
Below topics are covered in this article:
Introduction to Static keyword in Java
Applications of Static keyword
Static Variable
Static Method
Static Classes
Introduction to Static Keyword in Java
In Java, static keyword is mainly used for memory management. It can be used with variables,
methods, blocks and nested classes. It is a keyword which is used to share the same variable or
method of a given class. Basically, static is used for a constant variable or a method that is same
for every instance of a class. The main method of a class is generally labeled static.
Static Variable
When you declare a variable as static, then a single copy of the variable is created and divided
among all objects at the class level. Static variables are, essentially, global variables. Basically,
all the instances of the class share the same static variable. Static variables can be created at
class-level only.
Static Methods
When a method is declared with the static keyword, it is known as a static method. The most
common example of a static method is the main( ) method. Methods declared as static can
have the following restrictions: