As in the subject Can I mark object field as unique in Java? I want to have unique id in the class.
-
4The reference to the object is the object's unique id. – Marko Topolnik Nov 21 '12 at 13:05
-
2@PicklishDoorknob `hashCode` is not unique. – Marko Topolnik Nov 21 '12 at 13:07
-
@MarkoTopolnik well, if you manage it properly it is – tckmn Nov 21 '12 at 13:08
-
@PicklishDoorknob How do you "manage it properly"? – Marko Topolnik Nov 21 '12 at 13:08
-
@MarkoTopolnik override it for everything you are comparing – tckmn Nov 21 '12 at 13:09
-
Please tell us what you like to achieve with the uniqueness. Do you mean technically unique or unique in regard to any business entity? – Kai Nov 21 '12 at 13:09
-
@PicklishDoorknob There is no theoretically imaginable way in which you can make `hashCode` unique. – Marko Topolnik Nov 21 '12 at 13:10
-
@MarkoTopolnik [hashCode](http://en.wikipedia.org/wiki/Java_hashCode()) I think he wants something like the example here. – tckmn Nov 21 '12 at 13:10
-
@PicklishDoorknob No, he wants something unique. Can you find the word "unique" on that page? – Marko Topolnik Nov 21 '12 at 13:12
-
@MarkoTopolnik the first example on that page will always be unique unless the objects are the same, in which case it doesn't matter. – tckmn Nov 21 '12 at 13:12
-
@PicklishDoorknob hashCode returns an `int`. Integer has a defined range. What if you have more objects than Integers? How will uniqueness work? – Kai Nov 21 '12 at 13:14
-
@user714965 make a new function called `longHashCode()` that returns a `long`. But anyway, there's over 4 million different `int`s. – tckmn Nov 21 '12 at 13:15
-
@PicklishDoorknob It is a widely known fact that hash collisions are the norm, I really wonder how you missed that. – Marko Topolnik Nov 21 '12 at 13:15
-
@MarkoTopolnik Yes, I know, but this will probably work for his purpose. – tckmn Nov 21 '12 at 13:16
-
@PicklishDoorknob Actually, it most certainly won't work for his purpose. It is quite misguided advice to implement uniqueness with pseudo-randomness, or to implement hashCode as an auto-incremented int. – Marko Topolnik Nov 21 '12 at 13:20
4 Answers
You can create unique IDs with a static variable which you increment on each object creation and assign to your ID variable:
private static int globalID = 0;
private int ID;
public obj()
{
globalID++;
this.ID = globalID;
}
-
Rewrite with `private static final AtomicLong id` and `id.incrementAndGet`. – Marko Topolnik Nov 21 '12 at 13:18
No, that is not possible. You have to manage that in a Controller class.

- 38,985
- 14
- 88
- 103
-
@user1825608 Every `Object` has a `hashCode()` method. Override it and make it return your ID. – tckmn Nov 21 '12 at 13:08
When you don't care about the content of your unique field except for the fact that they have to be unique, you can use the solution by nukebauer.
But when you want to set the value manually and throw an exception when the value is already used by another instance of the class, you can keep track of the assigned values in a static set:
class MyObject {
private Integer id;
private static Set<Integer> assignedIds = new HashSet<Integer>();
public void setId(int id) throws NotUniqueException {
if (!this.id.equals(id) {
if (assignedIds.contains(id) {
throw new NotUniqueException();
}
assignedIds.add(id);
this.id = id;
}
}
}
By the way: Note that both options are not thread-safe! That means that when two threads create an instance / set an ID at exactly the same time, they might still get the same ID. When you want to use this in a multi-threading environment, you should wrap the code in a synchronized block which synchronizes on the static variable.

- 67,764
- 9
- 118
- 153
-
-
When you want to ensure that no value is used twice during the whole lifetime of the application, there is no way around it. When you want to allow reuse of IDs which were used by an instance which is no longer needed, their IDs need to be removed from the set manually. Unfortunately Java doesn't support destructors like C++, so you have to make sure to call some free() method on instances before they leave scope. – Philipp Nov 21 '12 at 13:35
-
1Java does support weak references, and this is precisely the use case for them. – Marko Topolnik Nov 21 '12 at 13:39
Your question was a bit vague, but I am going to attempt to answer it based on the limited information.
I want to have unique id in the class.
If you are looking to specify a Unique ID for the class for persistence through a well-known framework such as Hibernate, you can do so through the HBM mapping or @Id annotation.
If you are looking to ensure that an instance of a particular Class is unique (from a runtime perspective), then you should override the .equals method and do the comparison in .equals based on the field that you are using as the "unique id of the class". If you wanted some sort of development-time indicator that particular field is your "unique ID", you could always create a custom annotation.
Here is an excellet answer from a different StackOverflow post regarding how to override .equals and .hashCode using the Apache Commons Lang library. You can use this and just modify the .equals override to compare on whatever field you are using as your "Unique ID".

- 1
- 1

- 6,003
- 8
- 48
- 85