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

No members online now.

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top