Last Updated: 21 October, 2024
In Java, static keyword is a non-access modifier that is used to define variables, methods, blocks, and nested classes. All the static properties belong to the class. So it's also called a class member.
To access static properties outside the class, we don't need objects of the class. We can access all the static properties with the class name using the . (dot) operator.
For example,
class_name . static_property_name;
The Static keyword can be used with:
If we declare a variable with the static keyword, that variable is known as a static variable in Java. The static variable belongs to the class and is accessible outside the class using the class name (object creation is not required to access static variables), so it's also called a class variable.
Static variables share the same memory for all the instances of a class. These variables are stored in a space called metaspace in the JVM memory pool.
Static variables are initialized when a class is loaded into memory and are discarded when the classloader for that class is unloaded. They can be cleaned up, even duplicated in another class loader.
Example 1 : Static Variables in Java
Output
Emp ID: 101, Emp Name: John, Org Name: Xyz Pvt. Ltd.
Emp ID: 102, Emp Name: Shyam, Org Name: Xyz Pvt. Ltd.
After updating static variable:
Emp ID: 101, Emp Name: John, Org Name: Abc Technologies Pvt. Ltd.
Emp ID: 102, Emp Name: Shyam, Org Name: Abc Technologies Pvt. Ltd.
If we declare a method with the static keyword, that method is known as a static method in Java. A static method belongs to a class rather than an instance of a class, so it's also known as a class method.
In Java, if we have to perform some common functionality where object dependencies have not required those methods, we declare it a static method.
All static methods are accessible outside the class using the class name. For example, class-name.method-name();
Some important points about the static method:
A static method can contain only static properties, such as static variables and static methods.
A static method does not contain instance properties like instance variables and instance methods.
We cannot use this and super keywords inside the static method.
Example 2 : Static Methods in Java
Output
static display() methed called.
100
static show() methed called.
A block that is declared with the static keyword is known as a static block in Java. It is used for initializing static variables. The static block gets executed at the time the class is loaded into memory.
Java allows writing multiple static blocks in a single class. The multiple static blocks will be executed one by one in the same order in which they were written in the Java class.
An interface does not allow writing static blocks inside it.
Example 3 : Static Blocks in Java
Output
Static Block 1 is executed.
Static Block 2 is executed.
Static Block 3 is executed.
A class that is defined inside another class with the static keyword is known as a static nested class in Java. A static nested class cannot access the outer class's non-static properties but can access static properties. A static nested class is always accessed from outside with the outer class name.
Syntax: Static Nested Class in Java
Example 4 : Static Nested Class in Java
Here, we have a class named OuterClass that has only static properties such as a static variable, a static method, and a static class known as a static inner class in Java. As we can see, the static inner class has a static method, and that method accesses the static variable and static method of the outer class directly. A static inner class can be accessed from outside using the class name.
Output
OuterClass - static display() method
num : 20
In Java, static is mainly used for memory management because static properties are common to all instances of a class. Static properties occupy memory only once at class load time, whereas instance properties occupy memory each time we create the object.
From a memory perspective, static variables are stored in heap memory.
In the above diagram, there are two static properties: schoolName and schoolRegINo, which are common for each object. It will occupy memory only once. whereas the JVM allocates memory multiple times for each instance property (stuName, stuID) according to the number of objects created.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between an instance method and a static method in Java?
Ans. In Java, instance methods and static methods have the following differences, as given below in the table.
Instance Method | Static Method |
---|---|
An Instance method is a normal Java method that is created without the static keyword.
public void display(){} |
A Static method is always created with a static keyword.
public static void display(){} |
Memory is allocated multiple times whenever a method is called. | Memory is allocated only once at the time of class loading. |
It is specific to an instance, so it is known as an instance method. | It is specific to a class and common for all instances, so it is known as a class method. |
These methods always access with object reference.
Syntax: object.methodname(); |
These properties are accessed with class reference.
Syntax: className.methodname(); |
Can we declare static variables inside an abstract class?
Yes, an abstract class can have static variables.
Why is the Java main() method declared as static?
In Java, the main method is by default declared static because, for static properties, we don't need to create an object of the class. Suppose it is an instance method. We have to create the object of that class before calling the main method. In this way, the JVM creates an object first, then calls the main() method, but this process requires extra memory allocation for the object. That's why, from the memory management point of view, it is made static.