1

Is there more simple way to create unique id for many objects?

package creatingid;
public class CreatingId {

    static int id;
    int iD;

    CreatingId(){
        int tempId = id;
        this.iD = tempId;
        id = this.iD + 1;



    }
    public static void main(String[] args) {

        CreatingId[] test = new CreatingId[3];

        for(int i=0; i<3; i++){
            test[i] = new CreatingId();
            System.out.println(test[i].iD);
        }

    }

}
Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
  • 1
    [UUID](https://stackoverflow.com/questions/2982748/create-a-guid-in-java)? – Ivar Oct 27 '19 at 11:12
  • The only way this can get any "simpler" is if you shorten your constructor code to "this.iD = id++;` There are certainly other ways, perhaps even _better_ ways, of assigning a unique ID to every object of a class, but probably none so simple as what you're doing now. – Kevin Anderson Oct 27 '19 at 11:26

0 Answers0