how do i override valueOf for an enum?

I'm having troubles figuring out exactly how to override valueOf for an enum. ex:

publicenum foo{

a, b, c

foo(){}

publicstatic ? valueOf(String s){

return ?

}

}

What's the correct class for the ? so that it is overrides the method? Thanks!

[622 byte] By [galaxyya] at [2007-11-27 6:15:29]
# 1
I'm almost sure all enums are inherently int's (and if that doesn't work try long)... but why do want to override valueOf... it just seems strange.
corlettka at 2007-7-12 17:26:10 > top of Java-index,Java Essentials,Java Programming...
# 2
> I'm almost sure all enums are inherently int'swrong> (and> if that doesn't work try long)... also wrong :)>but why do want to> override valueOf... it just seems strange.@Op. You shouldn't try to override that method.
kajbja at 2007-7-12 17:26:10 > top of Java-index,Java Essentials,Java Programming...
# 3
> What's the correct class for the ? so that it is> overrides the method? Thanks!The return type is foo.Kaj
kajbja at 2007-7-12 17:26:10 > top of Java-index,Java Essentials,Java Programming...
# 4
I need to override it because sometimes my enum is being displayed as it's abbreviation (using foo.getAbbreviation() which I created as opposed to using foo.toString()).neither long nor int worked, but thanks.
galaxyya at 2007-7-12 17:26:10 > top of Java-index,Java Essentials,Java Programming...
# 5

"the return type is foo"

this didn't work when I put it in there:

public static foo valueOf(String s)

Overriding this method makes my enum much more robust. I want to try to first throw the string up to the super.valueOf and then if there is an exception use a different set of rules for returning....i hope that makes sense.

galaxyya at 2007-7-12 17:26:10 > top of Java-index,Java Essentials,Java Programming...
# 6

> "the return type is foo"

>

> this didn't work when I put it in there:

That's because you can't override it.

>

> public static foo valueOf(String s)

>

> Overriding this method makes my enum much more

> robust.

No it would't. It would actually make it less robust.

> I want to try to first throw the string up to

> the super.valueOf and then if there is an exception

> use a different set of rules for returning....i hope

> that makes sense.

Nope :)

kajbja at 2007-7-12 17:26:10 > top of Java-index,Java Essentials,Java Programming...
# 7

> That's because you can't override it.

lame!

> No it would't. It would actually make it less robust.

I want to allow the valueOf() to accept a wider range of String thus making it more robust. Now I have to make a whole separate method and a try/catch block for when the normal valueOf() throw an IllegalArgumentException (as it can't match the string to the enum). That's a lot of extra code and is less elegant imo :)

galaxyya at 2007-7-12 17:26:10 > top of Java-index,Java Essentials,Java Programming...
# 8
ps are you sure that I can't override it? Other methods can be overridden, for example toString().
galaxyya at 2007-7-12 17:26:10 > top of Java-index,Java Essentials,Java Programming...
# 9
> ps are you sure that I can't override it? Other> methods can be overridden, for example toString().Yes I'm 100% sure. It's a automatically generated method.Kaj
kajbja at 2007-7-12 17:26:10 > top of Java-index,Java Essentials,Java Programming...
# 10
alright, thanks kajbj!
galaxyya at 2007-7-12 17:26:10 > top of Java-index,Java Essentials,Java Programming...