aspose file tools*
The moose likes Beginning Java and the fly likes Rules roundup (#95) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rules roundup (#95)" Watch "Rules roundup (#95)" New topic
Author

Rules roundup (#95)

Raghuveer Kumarakrishnan
Ranch Hand

Joined: Mar 13, 2005
Posts: 32
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


Raghu<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780


There is no emoticon for what I am feeling!
Raghuveer Kumarakrishnan
Ranch Hand

Joined: Mar 13, 2005
Posts: 32
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

And Jeff's point is that you're wrong. Read the program he posted, then try to compile it.


[Jess in Action][AskingGoodQuestions]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Jan 30, 2000
Posts: 18671
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: 18671
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
Raghuveer Kumarakrishnan
Ranch Hand

Joined: Mar 13, 2005
Posts: 32
Jeff,Ernest and Jim, thanks for the clarifications.

[ November 17, 2005: Message edited by: Raghu Kumar ]
[ November 17, 2005: Message edited by: Raghu Kumar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rules roundup (#95)