在我的java程序中,我需要将最近的值存储在一个变量中,我的代码如下所示
public class Exmp2
{
int noOfInstances;
public Exmp2()
{
noOfInstances++;
}
public static void main(String[] args){
Exmp2 e1=new Exmp2();
System.out.println("No. of instances for sv1 : " + e1.noOfInstances);
Exmp2 e2=new Exmp2();
System.out.println("No. of instances for sv1 : " + e2.noOfInstances);
System.out.println("No. of instances for st2 : " + e2.noOfInstances);
Exmp2 e3=new Exmp2();
System.out.println("No. of instances for sv1 : " + e3.noOfInstances);
System.out.println("No. of instances for sv2 : " + e3.noOfInstances);
System.out.println("No. of instances for sv3 : " + e3.noOfInstances);
}
}
我的输出应该是 1 2 2 3 3 3 但我得到 1 1 1 1 1 1 你能给出解决方案吗?