I was searching for what static variable mean I find this site
http://java-questions.com/keywords-interview-questions.html
and its his declaration about static variable when I use his example I find different result
Static keyword can be used with the variables and methods but not with the class. Anything declared as static is related to class and not objects.
Static variable : Multiples objects of a class shares the same instance of a static variable.Consider the example:
public class Counter{
private static int count=0;
private int nonStaticcount=0;
public void incrementCounter(){
count++;
nonStaticcount++;
}
public int getCount(){
return count;
}
public int getNonStaticcount(){
return nonStaticcount;
}
public static void main(String args[]){
Counter countObj1 = new Counter();
Counter countObj2 = new Counter();
countObj1.incrementCounter();
countObj1.incrementCounter();
System.out.println("Static count for Obj1: "+countObj1.getCount());
System.out.println("NonStatic count for Obj1: "+countObj1.getNonStaticcount());
System.out.println("Static count for Obj2: "+countObj2.getCount())
System.out.println("NonStatic count for Obj2: "+countObj2.getNonStaticcount())
}
Output
Static count for Obj1: 2
NonStatic count for Obj1: 1
Static count for Obj2: 2
NonStatic count for Obj2 :1
when I use this Example I got
Static count for Obj1: 2
NonStatic count for Obj1: 2 // instead of 1
Static count for Obj2: 2
NonStatic count for Obj2 :0 // instead of 1
Can any one tell me what static variable mean and Example declare how can I use it in my method
thank you