Java- give every instance of object an unique number

后端 未结 2 922
一生所求
一生所求 2021-01-25 23:14

I\'m having trobule writing a simple program in java.

I have a class called ticket, where I have:

public class Ticket {
public String movieTitle = null;
         


        
2条回答
  •  清酒与你
    2021-01-25 23:58

    I think it is because you are using wrapper classes, i.e. Integer instead of int, which means that inside the class field you are only storing the reference. That means that in your constructor you are actually linking each class' currentMovieNumber to the static movieNumber so that when you increment the movieNumber, since all the instances' numbers point to it, every Ticket will have the same incremented number.

    Using int instead of Integer should solve this.

提交回复
热议问题