Java Static
Last modified on September 30th, 2014 by Joe.
Java Static Variables
- Java instance variables are given separate memory for storage. If there is a need for a variable to be common to all the objects of a single java class, then the static modifier should be used in the variable declaration.
- Any java object that belongs to that class can modify its static variables.
- Also, an instance is not a must to modify the static variable and it can be accessed using the java class directly.
- Static variables can be accessed by java instance methods also.
- When the value of a constant is known at compile time it is declared ‘final’ using the ‘static’ keyword.
Java Static Methods
- Similar to static variables, java static methods are also common to classes and not tied to a java instance.
- Good practice in java is that, static methods should be invoked with using the class name though it can be invoked using an object. ClassName.methodName(arguments) or objectName.methodName(arguments)
- General use for java static methods is to access static fields.
- Static methods can be accessed by java instance methods.
- Java static methods cannot access instance variables or instance methods directly.
- Java static methods cannot use the ‘this’ keyword.
Java Static Classes
- For java classes, only an inner class can be declared using the static modifier.
- For java a static inner class it does not mean that, all their members are static. These are called nested static classes in java.
[…] coming to the static import part. Like the above ugly line we have been unknowingly using (abusing) the java static […]
Good, concise answer.
But, I would want one more thing to be added to it:
Static methods can’t be overridden, which implies they are final methods. Rather, the concept of overridden itself should never arise because static methods are part of a class and not of an object.
But, because of the notation we abuse by writing obj.staticMethod(), people generally get confused.
Thanks,
hi…!
If you would use static methods and fields only, you would end up programming procedural like you would do with C or Pascal, loosing all the benefits of oop..so static method is used only when required and instance method is used elsewhere to get the benefit of oop.
nice tutorial good job
It is a nice website
very helpful website
thanks alot
[…] Instance Variable Java variables that are declared without static keyword are instance […]
[…] As given in the last line, method area is part of non-heap memory. It stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields. […]
A good tutorial !
Thanks for sharing.
[…] initialization block. That too you have seen in it a class for initialization. You may have used a static initialization block. When you use the initialization block for an anonymous inner class it becomes […]
Thanks to whomsoever it may concern..This is a good material,which seemed to be useful to me.
it is not clearly, please provide full information with example
[…] To specifically denote that the instance variable is used instead of static or local variable.That […]
@Anonymous:i cannot understand your point ll u please explain it
thanks
no access modifier – Subclasses in other package wont allow.
But in protected will can be accessed subclasses in another pacakge.
Anonymous on October 14th, 2010 12:47 pm
By last point in java Instance Variables
When the ” value of a constant is known at compile time ” it is declared ‘final’ using the ‘static’ keyword.
i would like to ask one thing according to double quotes
there are how many to give a value to a variable or a Constant
1. Compile time
2….
3…
there are how many possible ways?
thanks
what are static fields
what about this
Class A{//something here}
public Class B{
static A obj= new A(); // a static object to //class
****
can you explain this how this works?
nice one
Hi Joe,
I have doubt about Static.
When we go for static and why we need static? In which situation we need to go for static.?.
Thanks a lot for your Great work. your blog helped me to clear all level interview.
Hi Kannan,
Static are basically class variable, they are not stored on the heap but in the special area called method area, unlike object which when allocated occupied area in heap.
Static variable are loaded just once when the class is loaded by the JVM at the same time static variable is also created. So all the content which will be common to all the method and object are stored as static variable.
Shazz, thanks for replying to users questions.
[…] global access. It can be accessed from anywhere inside the thread. Also note that, it is declared static and […]
what is the difference between class variable and static variable ?
[…] a final class and cannot be instantiated. Therefore all its memebers (fields and methods) will be static and we understand that it is an utility class. As per javadoc, “…Among the facilities […]
Hi Joe,
I am a Java newbie. This page is very helpful.
The question I have is about Static Methods is…..’Java static methods cannot access instance variables or instance methods directly.’
Why in the Main() method which is a Static Method, we can access other methods (part of that class) that are not defined as Static?
This was Very Useful
static methods can only call other static methods/access static data …
[Complete Java Reference Ed2 Pg176]
But main is static, in which we call all static and non static methods.
How come?
hi joe,
can you explain about auto boxing&un boxing concept?
Java instance variables & methods are given separate memory for storage.what is the name for such type of specified memory
class PRO15
{
public static void main(String[] args)
{
static int i=10;
static int j=i;
System.out.println(i);
System.out.println(j);
}
}
why static method can’t be define in main??
1.As we know main method is static in java and there is some restriction for static keyword
2.static methods can call static methods and static variables only
3.so my question is this how main method can access the class variables and methods as they are not static
very nice
To Shilpi Agrawal,
As joe have written above that. staic method cannot invoke the instance method directly.. but it can invoked by creating an object of the class and then using . operator to invoke the method of the object created
please write some examples also that we can better understand the concepts….
sir we want u to explain the examples with coding b’coz we r the beginners in java…
You have mention all kind of terms very beautiful but I need example with coding so that I can explain very well. I am beginners in java.
[…] the library HelloWorld using static keyword. This library file will be compiled out of the C source in the coming […]
Can you please explan to me one real time scenario.Means where i want to use static variables.
Hey, You wrong buddy:
1)Using Instances or Obects you can’t access static variables and metods in Java.
2)To access the static variables and methods, you can do this by using (Classname.method/variable) or can directly access the method or variable without using the Classname.
3)You also stated “Static methods can be accessed by java instance methods.”
Yaa, here I agree, but what is the use and utility then , one example is below:
public class C {
static String a[] = { “2”, “3” };
public static void method() {
System.out.println(“Welcome to Java”);
}
public void met() {
C.method();
}
public static void main(String[] args) {
C c1 = new C();
c1.met();
}
}
Please Reply for this.
For my 1st point.. is appended in the last ” directly by . operator”).
For the 3rd point , we can do this directly using class name , right.
What is the basic difference between usage of static and final?
explain with simple exampe pls
can you include the example code for static
very-very…..good site for java teaching …..
thank a lot for this website …..
Very good website.. nice job.. keep helping .. :)
Thanks for this info joe……but why static methods are used????…can you please give me an example…thanks
As the java rules says that local variables can not be declared as static
so i,j are local inside main so it is error. If local variables are static they can not be shared among objects as it is not available in heap area where instance variables are stored.
When there is a requirement that all the instances of a class must have a common data with same value then we should declare static variable instead of instance variable as a single memory is available for static variables and can be shared among all the instances instead of multiple copy of variables as that of instance variables.
if we declare a variable as static that means multiple instance of a class has to share a single variable but the value of the variable can be changed. If we declare a variable as final its value can not be changed i.e it becomes a constant. One important difference is that local variables can be final but not static.
If we declare methods as static no need of creating object of that class methods can be directly called by class name. Good example is our main() method it is static so it is directly called on the class by jvm.
class A{
p s v m(String[] args){
}
}
while executing we ar writing in cmd as
java A
but actually it is
java A.main()
class A{
private static int x=10;//static
private int y=20;//instance
public static void main(String[] args){
A a1=new A();
A a2=new A();
a2.y=200;
System.out.println(a1.y);//20
System.out.println(a2.y);//200
a1.x=500;
System.out.println(a1.x);//500
System.out.println(a2.x);//500
}//main()
}//class
As static variables are shared among all the objects where as each object is having their separate copy of instance variable .
if the local variable is declared as static then it will be stored in the method area is what i have understood. in that case all the objects can access it.This contradicts your explanation.Please sort my doubt
Nice article…
Please mention about static block also.
That is the only thing remaining in this description on static…
class Amit{
static {
System.out.println(“This java program has run without the main method”);}
public static void main(String args[])
{}
}
Explain me this static() method.
Thanx
In order to use the class, that class should be available inside a memory ?
Mayuresh said on 07/02/2013,
Nice article…
Please mention about static block also.
That is the only thing remaining in this description on static…
what is static modifiers with program plz upload
ur answers are not statisfied for me
Gud..nice tutorial..
Hi Joe,
Could you please explain the need of having static classes? This question popped up when I had gone through implementation of LinkedList in java. It uses static class, I tried to create code similar to it without static nested class, still it is working. So I am puzzled with the usage of static nested classes.
it is really very helpful…
Hi,
Good post, I think we are missing the explanation for static blocks of code.
more information plz………….
STATIC can be:
Variable.
Method.
Block.
Nested class.
Non object.
static keyword.
this and super cannot be used in static context.
Why do we use Static methods at all when we can do the same thing with instance methods.For the argument that static methods declared final cannot be changed so can be the instance variable declared as final.Presuming that the static method can be used by a single object at any instance wouldn’t it be beneficial to create an instance method instead which can be instantiated any number of times and can be used by any number of objects.This would improve the performance as well.And for the argument that latter may create more objects there’s garbage collector for our help.
useful post
Thanks
can u give me full detailed explanation for static keyword
[…] file is compiled to a binary class, compiler inserts a field into java class file. It is a public static final field named ‘class’ of type […]
Mr Nanda
I understand your explanation here and it’s well put.But don’t you think the question lies partially unanswered? Can you give a real life instance where static variables would be useful? it’s oop after all.
static members will be executed at the tile of loading byte code of a class and will fallow the execution order which we have given in the class
static members will be executed at the time of loading byte code of a class and will fallow the execution order which we have given in the class