• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rules roundup (#95)

 
Raghuveer Kumarakrishnan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I think the explanantion for #95 needs to be modified a little

The case argument must be a int literal or int-compatible variable which is a constant (i.e. static final)



I think it sould read "...which is a constant (i.e. final)"

static modifier is not necessary here
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Raghuveer Kumarakrishnan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

My point was not that static wont work but static is not needed just final would do .

static final int -- works
final int --works
--------------------------
=> final is needed,static optional
-----------------------------
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Jeff's point is that you're wrong. Read the program he posted, then try to compile it.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghu Kumar:

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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Raghuveer Kumarakrishnan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,Ernest and Jim, thanks for the clarifications.

[ November 17, 2005: Message edited by: Raghu Kumar ]
[ November 17, 2005: Message edited by: Raghu Kumar ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic