Reputation: 6132
I have an enum:
public enum X implements Y
I also have a class
Ybuilder
which can create instances of Y
, let's say with Ybuilder.create(int value)
How can I set the enum values in my enum to be instances of Y
created with Ybuilder
? ideally it would be something simple like
public enum X implements Y {
A (Ybuilder.create(0)),
B (Ybuilder.create(1)),
};
Upvotes: 1
Views: 68
Reputation: 328775
I don't think you can do it that way - one alternative would be to delegate to the Y instance created by the builder. For example:
interface Y { //Assume Y has two methods
void m1();
void m2();
}
public enum X implements Y {
A (0),
B (1);
private final Y y;
X(int value) { this.y = YBuilder.create(value); }
//Then delegate to y to implement the Y interface
public void m1() { y.m1(); return; }
public void m2() { y.m2(); return; }
};
Note that I created the enums with the int
argument but you can pass a Y
directly if you prefer:
public enum X implements Y {
A (Ybuilder.create(0)),
B (Ybuilder.create(1));
private final Y y;
X(Y y) { this.y = y; }
};
Upvotes: 6