什么是静态变量
What is a static variable
我正在搜索静态变量的含义
我找到这个网站
http://java-questions.com/keywords-interview-questions.html
及其关于静态变量的声明
当我使用他的例子时,我发现了不同的结果
静态关键字可以与变量和方法一起使用,但不能与 class 一起使用。声明为静态的任何内容都与 class 有关,而不是对象。
静态变量:class 的多个对象共享同一个静态实例 variable.Consider 示例:
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())
}
输出
Static count for Obj1: 2
NonStatic count for Obj1: 1
Static count for Obj2: 2
NonStatic count for Obj2 :1
当我使用这个例子时,我得到了
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
任何人都可以告诉我静态变量的含义和示例声明我如何在我的方法中使用它
谢谢
你得到了错误的结果,因为你递增了 countObj1
两次:
Counter countObj1 = new Counter();
Counter countObj2 = new Counter();
countObj1.incrementCounter();
countObj1.incrementCounter();
// !!!
替换为:
Counter countObj1 = new Counter();
Counter countObj2 = new Counter();
countObj1.incrementCounter();
countObj2.incrementCounter();
现在,您得到了正确的结果:
Static count for Obj1: 2 //Counter.count
NonStatic count for Obj1: 1 //obj1.nonStaticcount
Static count for Obj2: 2 //Counter.count (the same as in the first line)
NonStatic count for Obj2 :1 //obj2.nonStaticcount
您正在对同一个对象调用 incrementCounter()
两次:
countObj1.incrementCounter();
countObj1.incrementCounter();
你应该这样做:
countObj1.incrementCounter();
countObj2.incrementCounter();
我正在搜索静态变量的含义 我找到这个网站
http://java-questions.com/keywords-interview-questions.html
及其关于静态变量的声明 当我使用他的例子时,我发现了不同的结果
静态关键字可以与变量和方法一起使用,但不能与 class 一起使用。声明为静态的任何内容都与 class 有关,而不是对象。
静态变量:class 的多个对象共享同一个静态实例 variable.Consider 示例:
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())
}
输出
Static count for Obj1: 2
NonStatic count for Obj1: 1
Static count for Obj2: 2
NonStatic count for Obj2 :1
当我使用这个例子时,我得到了
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
任何人都可以告诉我静态变量的含义和示例声明我如何在我的方法中使用它
谢谢
你得到了错误的结果,因为你递增了 countObj1
两次:
Counter countObj1 = new Counter();
Counter countObj2 = new Counter();
countObj1.incrementCounter();
countObj1.incrementCounter();
// !!!
替换为:
Counter countObj1 = new Counter();
Counter countObj2 = new Counter();
countObj1.incrementCounter();
countObj2.incrementCounter();
现在,您得到了正确的结果:
Static count for Obj1: 2 //Counter.count
NonStatic count for Obj1: 1 //obj1.nonStaticcount
Static count for Obj2: 2 //Counter.count (the same as in the first line)
NonStatic count for Obj2 :1 //obj2.nonStaticcount
您正在对同一个对象调用 incrementCounter()
两次:
countObj1.incrementCounter();
countObj1.incrementCounter();
你应该这样做:
countObj1.incrementCounter();
countObj2.incrementCounter();