If you want to have an identifier for an object that you create, you can simply have an instance of your object that holds the identifier of each object you create.
For example if you have a class called Car
, then that class can have a String
variable called id, which can be used with each Car object created to give that object an identifier. E.g.
class Car
{
private String id;
public Car(String id)
{
this.id = id;
}
}
Then now when you create a Car object, you pass a unique identifier for that object to the constructor:
Car car1 = new Car("c1");
If you then want to get the id of each car, you get use a getter method that return the id field of a specific car object. For example:
public String getId()
{
return id;
}
then you can get the id of car1 by doing this:
System.out.println(car1.getId());
Hope this is what you're looking for.