简体   繁体   English

Java程序的异常行为

[英]Unusual behavior of java program

I am new to Java and trying to learn it. 我是Java的新手,正在尝试学习它。 I wrote two class as follows, and I expect to print as 1, 2, 3 but it prints 3, 3, 3. I read a java book about I couldn't figure out the above behavior and print 1, 2, 3. 我写了两个类,如下所示,我希望将其打印为1、2、3,但其输出为3、3、3。

public class Student{
    private static int indexNumber = 0;

    Student(){
         indexNumber=indexNumber+1;
    }
    public void test() {
         System.out.println(this.getIndexNumber());
    }
    public int getIndexNumber(){
         return indexNumber;
    }
} 

public class College {
    public static void main(String args[]){
        Student student1 = new Student();
        Student student2 = new Student();
        Student student3 = new Student();

        student1.test();
        student2.test();
        student3.test();
    }
}

Can anyone help? 有人可以帮忙吗?

indexNumber is static, so it's "shared" between every instance of the class. indexNumber是静态的,因此在类的每个实例之间“共享”。

If you want a unique, incrementing ID, do the following: 如果需要唯一的递增ID,请执行以下操作:

static int nextID = 1;
int indexNumber;

Student() {
    indexNumber = (nextID++);
}

This makes indexNumber an instance field, so that each Student gets its own copy. 这使indexNumber成为一个实例字段,以便每个Student获得自己的副本。 The static variable is used to keep track of the next ID to be assigned. 静态变量用于跟踪要分配的下一个ID。

You get 3,3,3 because you have defined the variable indexNumber as static. 您得到3,3,3,因为您已将变量indexNumber定义为静态。 So when instantiating three student objects the indexNumber gets value 3. To increment define the indexNumber as instance variable and pass the value for it as parameter. 因此,当实例化三个学生对象时,indexNumber将获得值3。要递增,请将indexNumber定义为实例变量,并将其值作为参数传递。

Your field is static.It will be shared by all objects. 您的字段是静态的,它将被所有对象共享。

private static int indexNumber = 0;

s1-------->indexNumber<------s2
              ^
              |
              |
              |
              s3

Instantiating each time,the constructors gets called,which increments the indexNumber by 1. 每次实例化时,都会调用构造函数,这会使indexNumber增加1。

indexNumber is declared as static and its common to all objects. indexNumber被声明为static并且对所有对象都是公共的。 Hence you got 3,3,3 因此,您得到3,3,3

Static members are associated with the class, rather than with any object. 静态成员与类关联,而不与任何对象关联。

Static members are shared with all objects. 静态成员与所有对象共享。 You not indexed, you counted with your test. 您没有索引,您的测试算在内。

Since you have declared the INDEX NUMBER as static, therefore it will be shared by every instance you create for the class. 由于您已将INDEX NUMBER声明为静态,因此,您为该类创建的每个实例都将共享它。 The answer which you were expecting will come in the case you remove the static keyword. 如果您删除了static关键字,将会得到您期望的答案。 That's why for all three objects you get the same value for index number. 这就是为什么对于所有三个对象,您获得相同的索引号值的原因。 I hope it's clear to u now. 我希望你现在很清楚。 Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM