Let's say we just have a simple object Person with int ID which identifies it. How can I give new ID value (+1) for each new instance of Person but in constructor of that class Person? (I use no DB for this)
Asked
Active
Viewed 338 times
1
-
1@Mudu-s answer is thread-safe, while all other answers (which are exact duplicates including my answer, albeit mine was first) aren't. – Luka Ramishvili May 08 '12 at 13:14
5 Answers
9
Use a static AtomicInteger
:
final class Foo {
private static final AtomicInteger seed = new AtomicInteger();
private final int id;
public Foo() {
this.id = seed.incrementAndGet();
}
}
See here for more information: https://stackoverflow.com/a/4818753/17713

Community
- 1
- 1

Matthias Meid
- 12,455
- 7
- 45
- 79
1
use static variables; static variables aren't bound to class instances, rather to classes directly.
Example (in C#):
public class Person{
public static int Increment = 1;
public int ID;
public Person(){
this.ID = Increment;
Increment++;
}
}
This way all class instances will have unique ID-s (incremented by 1).
EDIT: This approach isn't thread-safe, see @Mudu's answer.

Luka Ramishvili
- 889
- 1
- 11
- 20
-
1Except that's not thread-safe, nor is it a good idea to have public variables, nor is it following Java naming conventions. – Jon Skeet May 08 '12 at 13:12
-
1Bear in mind that this is not thread-safe, but fine as long as you're not using multiple threads. – Matthias Meid May 08 '12 at 13:12
-
1This solution doesn't take into consideration the threaded invocation of `Person` constructor. – Sanjay T. Sharma May 08 '12 at 13:13
-
1@JonSkeet - in his defence he has stated it is a C# example, I think you could offer a Java answer yourself? – Neil May 08 '12 at 13:13
-
1@Neil: True - I missed that, having seen that it's a Java question. It's still not thread-safe or following good design principles in C# though :) (There's already a good Java answer suggesting AtomicInteger.) – Jon Skeet May 08 '12 at 13:14
-
1@Mudu JonSkeet I commented on OP's question, I know it's not thread safe, that's why Mudu's answer was better. – Luka Ramishvili May 08 '12 at 13:16
-
1I didn't edit my answer because Mudu already updated his with a working example. – Luka Ramishvili May 08 '12 at 13:17
-
1@LukaRamishvili That's perfectly fine too, thread-safety is not needed everywhere, and the OP did not explicity as for it. I just found it worth being mentioned. :) – Matthias Meid May 08 '12 at 13:17
-
1Yep, anyway, good job. @JonSkeet my fault, didn't see the java tag or I would post a java answer :) – Luka Ramishvili May 08 '12 at 13:18
-
Well I was only trying to figure out possible ways of achieving this, it wasn't specific. Thank you though for the multi thread warning. – vedran May 08 '12 at 13:33
1
You should use something like
public class YourClass {
private static int generalIdCount = 0;
private int id;
public YourClass() {
this.id = generalIdCount;
generalIdCount++;
}
}

javatutorial
- 1,916
- 15
- 20
1
Use static counting field which is shared accros all instances of Person
:
class Person {
private static int nextId = 1;
private final int id;
Person() {
id = nextId++;
}
}

Victor Sorokin
- 11,878
- 2
- 35
- 51
1
You could create a static variable for the current counter value, and assign that to the ID when created...
public class Person {
// same across all instances of this class
static int currentCounter = 0;
// only for this instance
int personId;
public Person(){
personId = currentCounter;
currentCounter++;
}
}

wattostudios
- 8,666
- 13
- 43
- 57
-
1True. I guess its up to the author to choose the answer which is most appropriate for their context, although a thread-safe answer like @Mudu will allow for all contexts regardless. Thanks for your feedback. – wattostudios May 08 '12 at 13:18