Bruno Jácome
Bruno Jácome

Reputation: 89

Java: Enum class constructor

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.

Upvotes: 1

Views: 156

Answers (2)

Enzo Caceres
Enzo Caceres

Reputation: 519

You can call one constructor form another.

Take a look at: How do I call one constructor from another in Java?

Grass(1),
GrassTall(2),
GrassFlower1(3),
GrassFlower2(4),
Water(5),

GrassTwig(6, true),
GrassRock(7, true);

TileID(int id) {
    this(id, false); // false = default value of 'collidable'
}

TileID(int id, boolean collidable) {
    this.id = id;
    this.collidable = collidable;
}

Moreover, you don't have to specify ids everytime. You can:

  • Use a static int that you increment
  • Use the Enum#ordinal() that will return what 'position' a value is in the enum

ABOUT YOUR EDIT:

There is a quicker method if you use the Enum#ordinal() to quickly retreive a corresponding enum. By doing:

public static TileID fromId(int id) {
    return TileID.values()[id];
}

But be aware that you might want to protect this function against negative or too large values.

Upvotes: 7

user
user

Reputation: 7604

Sure, you can always set the collidable field according to the id using if or switch, as long as there aren't any constants with the same id field but different values for the collidable field.

Just do

TileID(int id) {
    this.id = id;
    this.collidable = id > 5; //or some other condition
}

And then omit the second argument in your constants (Grass(1), GrassTall(2), ...;).

Upvotes: 3

Related Questions