All About "static"
Static means “no change”. In Java static is used with a variable or a class which is same for every instance. The variable marked as static is common for all the instances.
So why do we use static keyword, you ask?
It is used for optimizing the memory. By this I mean is that instead of making a separate copy of the same thing we can store it in one place and share it with others.
The static elements belong to the class. We do not have to instantiate the class to invoke the method or access the variable. We can simply write, class_name.variable or class_name.method_name. The main() method in a Java program is static because the program execution starts from here. So, this method is invoked without any object creation.
We can use the static keyword with:
- variables
- methods
- block
- nested class
Static variable or class variable
A variable declared as static is also called as class variable as we can directly access it using the class name:
class_name.statiVariable
You can change the value of a static variable. This is one of the main difference between a final and static variable:
public class StaticVariableExample {
public static String SCHOOL_NAME = "KV";
public static void main(String args[])
{
System.out.println(StaticVariableExample.SCHOOL_NAME);
SCHOOL_NAME = "DY";
System.out.print(StaticVariableExample.SCHOOL_NAME);
}
}
The following program is a simple counter example:
package counterexample;
public class CounterExample {
public static int COUNT = 0;
CounterExample()
{
COUNT++;
System.out.print(COUNT);
}
public static void main(String[] args) {
CounterExample c1 = new CounterExample();
System.out.println(c1);
CounterExample c2 = new CounterExample();
System.out.println(c2);
}
}
package counterexample;
public class CounterExample {
public static int COUNT = 0;
CounterExample()
{
COUNT++;
System.out.print(COUNT);
}
public static void main(String[] args) {
CounterExample c1 = new CounterExample();
System.out.println(c1);
CounterExample c2 = new CounterExample();
System.out.println(c2);
}
}
This gave the following output:
1
2
The COUNT variable will retain its value. If it is non-static variable then the output will be 1.
2
The COUNT variable will retain its value. If it is non-static variable then the output will be 1.
Static method or class method
A static method is also called as a class method as we can directly invoke them without creating an object.
A static method can only access the static variables. Let's understand this by the following example:
package staticmethod;
public class StaticMethod {
public static void main(String[] args) {
StaticMethod.helloWorld();
}
public static void helloWorld()
{
System.out.println("Hello World");
}
}
One of the main point to notice is that the static method cannot access a non-static data member:
Static block
The code in the static block is executed only once.
public class StaticBlock {
static int n1;
static String name;
static{
n1 = 5;
name = "five";
System.out.println("Static block");
}
public static void main(String args[])
{
StaticBlock sb = new StaticBlock();
StaticBlock sb2 = new StaticBlock();
}
}
Here is the output:
Static nested class
Let's see what happens when we make a class static:
So, this implies that we cannot make a top level class static. Now, let's create an inner class or a nested class and make it static:
As you can see, I am not getting any error here. That means we can create a static nested class.
public class StaticClassExample {
public static class NestedClass
{
public void displayMessage()
{
System.out.println("This is nested class");
}
}
public static void main(String[] args) {
NestedClass nc = new NestedClass();
nc.displayMessage();
}
}
Here is the output I got after executing this program:
I tried accessing the displayMessage() using the object of the outer class but I got an error. We cannot access elements of the static nested class using the reference of the outer class.
Conclusion
The static keyword is quite popular among the programmers. Many of us do not exactly understand the use of it 😂😅
I hope this article will help and if you want to make any correction to this article please comment down below.
Thank you!


