overriding enum constants toString

J

jemptymethod

can anybody please point me to some sort of example or documentation
for overriding toString for (individual) enum constants. the javadoc
indicates that this should be possibly but it is not entirely clear to
me how to do so.

thanks in advance
 
P

PaullyB

can anybody please point me to some sort of example or documentation
for overriding toString for (individual) enum constants. the javadoc
indicates that this should be possibly but it is not entirely clear to
me how to do so.

thanks in advance

Here is an example:

public class TestEnum {

enum MyEnum {

MYCUSTOMVALUE {
@Override
public String toString() {
return "My Enum String";
}
},
MYDEFAULTVALUE;


}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(MyEnum.MYDEFAULTVALUE.toString());
System.out.println(MyEnum.MYCUSTOMVALUE.toString());
}

}


Output:
MYDEFAULTVALUE
My Enum String
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top