I have an Enum class with two variables: int id
and boolean collidable
.
Is there a way to make a constructor that only receives the first variable and correctly fills the second one? Note that the first class variable is the id and it's different for every type value of the Enum.
public enum TileID {
Grass(1,false),
GrassTall(2,false),
GrassFlower1(3,false),
GrassFlower2(4,false),
Water(5,false),
GrassTwig(6,true),
GrassRock(7,true);
private final int id;
private final boolean collidable;
TileID(int id, boolean collidable) {
this.id = id;
this.collidable = collidable;
}
public int getId() {
return id;
}
public boolean isCollidable() {
return collidable;
}
}
Edit:
I wanted to know if there is a way to access directly the value corresponding to a given id, but I guess there isn't. I found this method on a different post and I think I'll be doing this instead.
public static TileID valueOf(int id) {
return Arrays.stream(values())
.filter(legNo -> legNo.id == id)
.findFirst().orElse(null);
}
Thanks to everyone that helped.