My point was not that static wont work but static is not needed just final would do .
You are right: "static" is not necessary, but "final" is not sufficient. I went and looked up the definition in the JLS -- it's one of those things that seems easy until you have to define it. It would seem that the roundup question, if meant to be read as a definition, is too vague.
The definition states that the case expression must be a constant expression or an enum expression (for 1.5). A constant expression may contain constant variables. A constant variable is of primitive or string type, final and initialized with a compile-time constant expression.
You are right, no mention of static.
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Well, Jeff didn't say what his point was, but his program does not show that static is necessary. Rather, it shows that a constant must be initialized as it is declared, using an expression which is itself a compile-time constant expression. Note that in Jeff's program, neither B nor C can be used as case options. It doesn't matter that C is static - the problem is it's not initialized on declaration. Conversely, if we modify the declaration of A to remove the "static", it still works fine. Being static is not necessary to be an option in a switch statement.
It would be true to say that there's no reason not to make such a variable static. Being final and also initialized on declaration with a compile-time constant means there's no possible way different instances would have different values. So I would certainly recommend making the variable static, for clarity if nothing else.
"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Obviously I saw Jeff's second post after writing the above, but I'll leave my post as is, as it has some supplemental discussion.
Raghuveer Kumarakrishnan
Ranch Hand
Joined: Mar 13, 2005
Posts: 32
posted
0
Raghuveer Kumarakrishnan
Ranch Hand
Joined: Mar 13, 2005
Posts: 32
posted
0
Jeff,Ernest and Jim, thanks for the clarifications.
[ November 17, 2005: Message edited by: Raghu Kumar ] [ November 17, 2005: Message edited by: Raghu Kumar ]