I have been trying to generate unique IDs for class instances starting from 1, like so:
public abstract class foo {
static int ID = 0;
final int id;
public foo() { this.id = ++ID; }
}
I then realised that I have several classes that require this behaviour, and so I tried to make an abstract class
from which to inherit, but I found that static
fields can't really be inherited.
Is there any way I can reuse this common behaviour in Java?
Edit:
I did not make it clear, but I need the indexes for each subclass to be not only unique, but also consecutive.
For example, if I have bar
and baz
inheriting from foo
:
bar b1 = new bar();
baz b2 = new baz();
bar b3 = new bar();
b2
should have id == 1
, and b3
should have id == 2
.
Not really! There are a couple of things you might try:
IDGenerator
class with a static method to generate a new ID for a specific class. The .generate()
method would take a Class<?>
parameter, and would maintain a HashMap
mapping classes to most recently given out identifiers. This would put all the logic into one class, but wouldn't really simplify things.IDGenerator
class inside each class that wants to use this kind of behaviour. In this case, the IDGenerator
class wouldn't need a Class<?>
parameter because there would be one instance for each class using it. Its .generate()
method would be a no-arg method. Again, I'm not sure this makes things much simpler.IDGenerator
with a static no-arg .generate()
method that would just dish out the next unused identifier. Your identifiers would then be unique across your application, and not consecutive within a class.What you've got is pretty fool-proof and slimline.
One more minor note: you say you want your IDs to start from 0, but the way you've written it, they'll start from 1. You want this.id = ID++
rather than this.id = ++ID
.