Introduction
In this article, we discuss static keywords in Java.
Static Keyword
The main use of static is for memory management. It can be be used in 3 ways:
- Static method
- Static variable
- Static blocks
1. Static method
When we use the static keyword with a method in the class it is known as a static method.
- It can be invoked without the need for creating an instance of a class.
- It belongs to the class rather than an object of a class.
- It can call only other static methods and cannot call a non-static method from it.
- It can access a static data member and it cannot access non-static data.
- It can't refer to the "super" and "this" keywords anyway.
Example
In this example, we create a static method to print the value of the static variable y.
- class StaticMethodEx
- {
- int x; //set to zero
- static int y; //when class is loaded y is set to zero but not other object created.
- StaticMethodEx()
- {
- //Constructor incrementing static variable y
- y++;
- }
- public void showvalues()
- {
- System.out.println("x = "+x);
- System.out.println("y = "+y);
- }
- //public static void incrementvalue()
- {
- //x++;
- //
- }
- }
- class OutputClass
- {
- public static void main(String args[])
- {
- StaticMethodEx s1 = new StaticMethodEx();
- s1.showvalues();
- StaticMethodEx s2 = new StaticMethodEx();
- s2.showvalues();
- //StaticMethodEx.y++;
- //s1.showvalues();
- }
- }
Output

2. Static variable
When we declare any variable in our program as static, it is known as a static variable. Some properties of static variables are:
- It can get memory only once in the class area at the time of class loading.
- It can be used to refer to the same property of all objects, in other words, a single copy is shared by all instances of the class (that is not unique for each obj).
- It can be accessed directly by the class name and doesn't need an object
Example
In this example, sv1 is the instance of the object, in this (initial) the value of a is 1 and the value of b is 8, according to the arithmetic operations in the constructor. Now, in the next instance of the object StaticVariablesEx is sv2. Here, the value of a is 2 because of the static variable while the value of b is again 8, because of non-static. In the final instance of the object StaticVariablesEx, the a is 3 while the b is 8.



Sam HobbsPosted May 11, 2013, 5:19 PM
But note that C++ and C# static methods can access non-static data. So theoretically there is not a problem. Are you totally sure that Java is different?
Sandeep SharmaPosted May 9, 2013, 11:04 PM
Yes, you are right sir. Static method is class-level thing, they do not belongs to any particular instance/object. Because of that they are not given any instance reference. So that's why static method can't access non-static method/data.
Sam HobbsPosted May 9, 2013, 2:08 PM
In C++ and C# static methods can access non-static data; they just need a pointer or reference to the instance of the class. It is unfortunate that Java does not allow that.