aspose file tools*
The moose likes Java in General and the fly likes Doubt about Switch statement (Compile Time Constant) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt about Switch statement (Compile Time Constant)" Watch "Doubt about Switch statement (Compile Time Constant)" New topic
Author

Doubt about Switch statement (Compile Time Constant)

Rahul Bansode
Greenhorn

Joined: Nov 03, 2011
Posts: 18
I have a doubt with the switch statement.

A switch's expression must evaluate to a char, byte, short, int, or, as of Java
6, an enum.
& the case constant must be a compile time constant!
Since the case argument has to be resolved at compile time, that means you can use only a constant or final variable that is assigned a
literal value.


Now why does the below statement fails ??



final int a = 1;
final int b;
b = 2;
int x = 0;
switch (x) {
case a: // ok
case b:
// compiler error


If I put SOUT statement below line (b=2), Value of b is printed as '2'.
then why doesn't switch statement accept it??

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Suppose say if 'b' is accepted and below code compiles without error; when you will think case b: will get executed?

Rahul Bansode
Greenhorn

Joined: Nov 03, 2011
Posts: 18
The above code is not getting compiled.....!!!

It's giving an error on case : b as "Case expressions must be Constant Expression"...!!!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
That was the point I was making - if it gets compiled, you will be able to change the value of b in the case a. And it will result in case b never executed.

Hence case statements can be compile time constants.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3658
    
  15
John Jai wrote:That was the point I was making - if it gets compiled, you will be able to change the value of b in the case a. And it will result in case b never executed.

Hence case statements can be compile time constants.

Your example doesn't address Rahul's question. In Rahul's original example, b was a final variable, so you wouldn't be able to change its value in case a. The only difference between a and b in Rahul's example was how they were initialised.


Joanne
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Thanks Joanne... I was mistaken on the question... I understood that now...
Rahul Bansode
Greenhorn

Joined: Nov 03, 2011
Posts: 18
Hi Joanne....!!

If I initialize the final variable at the time declaration (final int b = 2), The Switch case expression works fine.....!!!

but shows compilation error for Switch case expression




Hope you understand my question???


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

It could have been designed to allow that - as you say, it could interpret b as a compile-time constant. But in that case the compiler's got to do more work. As things are now all it has to do is check the line the variable is declared on. To allow your code it would have to tell the difference between that and:
and similar constructions.

Added to that - the only reason you ever need to initialise a final variable on a different line to where it's declared is if the value is determined at run-time. Since there's no good reason for you to write the code like that, there's no good reason for the compiler to support it.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3658
    
  15
Rahul Bansode wrote:Hope you understand my question???

Yes. I understood what you were asking. I just wasn't sure for certain what the answer was. What Matthew says is likely to be the reason, but if you want to know for certain why it works that way, you will probably need to read the Java Language Specification. Personally I would just accept that that is the way it is and not worry too much.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Rahul Bansode wrote:If I initialize the final variable at the time declaration (final int b = 2), The Switch case expression works fine.....!!!

but shows compilation error for Switch case expression



Hope you understand my question???



The answer is simple. The "b" variable is *not* a compile time constant. The JLS has many requirements for a variable to be a constant variable, and having it initialized during declaration is one of those requirements.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

this doesn't address the question(sorry), but i just wanted to mention that as of java7 we can use Strings also.


SCJP
Visit my download page
Rahul Bansode
Greenhorn

Joined: Nov 03, 2011
Posts: 18
Hi,Joanne .
I got it .

Thanks alot guys for your discussion!!!







Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Rahul Bansode wrote: . . . A switch's expression must evaluate to a char, byte, short, int, or, as of Java 6, an enum. . . .
It has accepted enums since Java5. Now, as Randall Twede has already told you, a String can be used.
In the older version, you could consider it an int, or anything which can be explicitly cast to an int.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt about Switch statement (Compile Time Constant)