Re: I don't understand enums
- From: Lasse Reichstein Nielsen <lrn@xxxxxxxxxx>
- Date: Sat, 26 Jan 2008 23:57:17 +0100
ptomblin+netnews@xxxxxxxxx (Paul Tomblin) writes:
public static final int PLAYLISTTYPE_FEATURE = PlaylistType.FEATURE.getID();....
Note that they're still "public static final".
But now case statements that worked before the change are breaking with
Eclipse complaining that "case expressions must be constant expressions".
Huh? They sure look constant to me.
They are not "constant expressions" in the sense of the Java specification,
which means *compile time* constants. What you have is a method call, on
an object from another class. There is no way to know what that might
return when it's executed (remember, you might recompile PlaylistType
and not Playlist, and it should still work).
How could it think that
PLAYLISTTYPE_FEATURE could change values when it's declared final?
It's not that it can't change during one execution of the program,
but that it's no a compile time constant that can be inlined in the
switch statement implementation.
However, you can switch on enums too, so change the switch to do
switch (playlistType) {
case PlaylistType.FEATURE: // ...
case PlaylistType.TRAILER: // ...
}
(The compiler will, internally, assign a compile time constant to each
enum value and do the switch on that instead.)
/L
--
Lasse Reichstein Nielsen - lrn@xxxxxxxxxx
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
.
- Follow-Ups:
- Re: I don't understand enums
- From: Lew
- Re: I don't understand enums
- Prev by Date: Re: I don't understand enums
- Next by Date: Re: Graphics help please
- Previous by thread: Re: I don't understand enums
- Next by thread: Re: I don't understand enums
- Index(es):
Relevant Pages
|
Loading