I want to refactor my enums in the following way: I have enums with method for getting its String value:
public enum Type {
TYPE_1("Type 1"),
TYPE_2("Type 2");
private String value;
Type(String value) {
this.name = name;
}
public String getValue() {
return value;
}
}
I want to get its value simply by toString()
method call. According to java.lang.Enum
code toString
is defined as
public String toString() {
return this.name;
}
So I guessed all I needed was to redefine name variable in my class:
public enum Type {
TYPE_1("Type 1"),
TYPE_2("Type 2");
private String name;
Type(String name) {
this.name = name;
}
}
But according to debugger there are 2 name variables: in my Type class and in Enum superclass, so toString()
returns Enum.name
My question is can I return Type.name
in Enum.toString()
value without redefining it in subclass?