When ever we declare a variable as "static" in a class,then, only one copy of it is shared by all objects of that particular class.
That means,any changes done by one object for static variable,is reflected in other objects also.
Suppose,there is a static variable x in class A.
and two objects,obj1,obj2 are created for a class A.
If obj1 changes the static var "x",that change is reflected in "x" of obj2 also.
see the following code,
class A
{
static int x=10;
static void show()
{
System.out.println("show in class A");
x=x+10;
System.out.println("x after modification in show() is "+x);
}
}
class statVarDemo
{
public static void main(
String args[])
{
int i;
A obj1=new A();
A obj2=new A();
System.out.println(" static var x is"+obj1.x);
obj1.show();
System.out.println("now x in obj2 is "+obj2.x);
}
}
Here x is declared as static and its value is modified in show() method.
Show() is called on obj1.So obj1.x after show()method call is 20.
And obj2.x also prints 20 as x is static var(only one copy for all instance var).
If we remove 'static" modifier from x and from the method show(),
the o/p of obj2.x is 10.
because each instance of class A have its own copy of x.
So,though,obj1 changes value of x,its not reflected in x of obj2.
Hope Iam clear.