0

I am trying to generate unique auto increment id which will started from 0. I found something for doing this using below code:

private static final AtomicInteger count = new AtomicInteger(0);   
uniqueID = count.incrementAndGet(); 

I can create auto increment ID by manually :

For example :

int i = 0; // Obviously I will declare this variable as global
++i;

In this way I can manage to do this. But I don't want to do this manually. I am finding some java method which will take of all this stuff.

And I found below code :

private static final AtomicInteger count = new AtomicInteger(0);   
uniqueID = count.incrementAndGet(); 

Now my question is, Is this the right way to generate unique ID? Or, is there any other better solution?

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94

1 Answers1

0

If it should be unique only within a single process, then yes you can use an AtomicInteger.

Layansan
  • 46
  • 5