Java static Variable Method
Summary: By the end of this tutorial "Java static Variable Method", you will comfortable to write programs with static variables and methods.
After discussing the usage of "this" keyword, let us move to another keyword "static". The "static" keyword is used as an access modifier. Java has access specifiers and access modifiers and both are very different. Access specifiers specify the access and access modifiers modifies the access. It may be confusing right now and will be clear soon.
Generally, you know earlier, to call a method and also a variable from the static main(), you require an object. Of course it is right but not with static methods and static variables. static modifier modifies the access and allows to call without the help of an object. "static" keyword can be applied to instance variables and methods and not with classes and also not with local variables..
Static with Variables
Java permits to call a static variable or method in three ways.
- Can be called without the help of an object
- Can be called with the help of an object
- Can be called with class name
The following program explains the static keyword with variables.
Example on static variables usage (of Java static Variable Method)
public class StaticDemo
{
static int price = 100;
public static void main(String args[])
{
System.out.println(price); // without object
StaticDemo sd1 = new StaticDemo();
System.out.println(sd1.price); // with object
System.out.println(StaticDemo.price); // with class name
}
}

The instance variable price is declared as static and is called in three ways – without object, with object and with class name.
Example on Static methods usage (of Java static Variable Method)
The same style of static variables is followed with static methods also and can be called in three ways. Following program explains.
public class StaticDemo2
{
public static void show()
{
System.out.println("Hello World");
}
public static void main(String args[])
{
show(); // without object
StaticDemo2 sd1 = new StaticDemo2();
sd1.show(); // with object
StaticDemo2.show(); // with class name
}
}

The show() method is declared as static and is called in three ways. It is allowed with class name also to facilitate to call many methods of Java API classes without the help of an object. Many methods are declared static in Java classes like java.lang.Character and java.lang.Math etc.
What is Class Variable? (of Java static Variable Method)
A static variable is also known as "class variable" as all the objects of the whole class refer (or share) the same variable (location); that is, one variable is used by all the objects (in encapsulation, each object will have separate location of a variable). As all the objects refer the same location (variable), no encapsulation exist with static variable.
Can a local variable be declared static? (of Java static Variable Method)
A local variable cannot be static. If static the meaning of static is lost. A static variable scope is for the objects of whole class and if declared local as static, the purpose of static is lost and compiler raises error.
Why the main() is static? (of Java static Variable Method)
main() method is declared as static to allow the JVM to call the main() without the help of an object. JVM is a separate process and to call the method of another process (your program), it requires an object. As the execution starts from the main(), the JVM cannot create an object without entering the main() method. If the main() method is allowed to call without the need of an object, the JVM can create hundreds of objects once enters the main(). For this reason, main() is declared as static.
Memory Allocation for static Variables
We know earlier every object will have its own copy of variables. But with static variables, it is very different. A static variable location is shared (or accessed or pointed) by all the objects of the class. The result is, if one object changes the value, all the other objects also get affected. With static variables, no encapsulation exists. Observe the following program.
public class Demo
{
static int x = 10;
public static void main(String args[])
{
System.out.println(x); // 10
Demo d1 = new Demo();
Demo d2 = new Demo();
System.out.println(d1.x); // 10
System.out.println(d2.x); // 10
d1.x = 20;
System.out.println(d2.x); // 20
d2.x = 30;
System.out.println(d1.x); // 30
System.out.println(x); // 30
}
}

Variable x is declared as static. Objects d1 and d2 point to the same x location. Observe, if d1 changes d2 and if d2 changes d1 are getting affected. Because all the objects of the whole class share the same location, the static variable is known as "class variable".
Thanx a lot sir , reallllly u have done superb explanation here. I’ve ever got these kinds of simply knowledge of complicated topics.
Always HELP with this kind of efforts to us.
Sir, whr does static methods and static variables are stored in memory…if its jvm specific then i m talking about sun specific
Hello sir… I am not clear with Class Variable…
plz explain….and thx in advance..!!
Now I slightly modified the notes. Read once more.
Sir, i am still confused why main() is static?If the main() method is allowed to call without the need of an object, the JVM can create hundreds of objects once enters the main(). For this reason, main() is declared as static.
i can’t understand this things.please help me sir.
Your program, say, a Demo program and JVM are two separate processes in the RAM. We know execution starts from main() method. To call the main(), JVM requires an object because JVM is an outside (separate) process not connected to Demo process. So to call main(), JVM requires an object. How JVM gets an object of Demo unless it enters into the execution space of Demo. The execution space of Demo contains main(). For this reason, main() is declared static to allow the JVM to call main() without the need of an object.
Still you do not understand, take the help of colleague programmer or if you are at Hyderabad, meet me.
Thanks a ton sir, why main() is static? now everything is clear.
Sir, could you suggest me some exercise questions for practice.
You will get within a week.
Thank you so much sir. Thanks a lot.
Hello sir,
please give me some exercise questions…you told me that i’ll get within a week.sir please.
I am sending to your mail right now.
Hi,
I am not clear with the transient and volatile keyword……Could you please explain it in a clear cut way??? Waiting for your valuable replies………
Regards,
smpth
transient variable cannot serialized (for this you must know what is serialization). volatile variable is that one whose value may get changed (to the maximum extent) in the remaining part of program.
static key word can be used with which access specifiers? and why?
Static can be used with all access specifiers. Only thing is static methods cannot be overridden and a constructor, local variables and classes cannot be static.
but we can declare a class static if it is inner class.
Nice explanation. Please add Collection and Thread concepts for new learners.
They are already there.
Sir why the local variables and class are not static can u plz elaborate it clearly???
Static, in Java, is known as access modifier. Access modifier modifies the access. Let us see what it modifies. A static variable and a static method can be called without the help of an object and moreover, a static variable will have only location that can be accessed by all the objects of the class. Local variable scope and life is within the method where it is declared. How can a local variable will have only one location that is accessed by all objects? To use a class, its object is to be created with new keyword. How can you call a method without the help of an object? Object identifies for which class the method belongs. JVM searches that class and loads it.
OK tanq sir,the static variable and static method can be called with the help of an object and with out object also then what is the difference between these two callings(with object and with out object)
Without object helps you to call in the same class. Suppose you want call in a different class by composition, then how you call? For the method you asked, where the JVM search.
pls any one tell me clearly suppose I’m not use “static”in my main method what will happen
Your program will be compiled but not executed.
ok