aspose file tools*
The moose likes Beginning Java and the fly likes constant inside the switch case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constant inside the switch case" Watch "constant inside the switch case" New topic
Author

constant inside the switch case

Bruno Sant Ana
Greenhorn

Joined: May 17, 2012
Posts: 29
Hi,

I don't understand why these two blocks of code doesn't compile.

In this first block does the compiler complain because I declared b in one line and assigned a value to it in another? At compile time isn't the compiler able to see that b references the value 2?



In this second block why an Integer variable isn't considered constant by the compiler?



thanks fellows
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Bruno Sant Ana wrote:
In this first block does the compiler complain because I declared b in one line and assigned a value to it in another? At compile time isn't the compiler able to see that b references the value 2?



In this second block why an Integer variable isn't considered constant by the compiler?





Java has a very specific definition of what is a compile time constant....

http://www.coderanch.com/t/454384/java/java/compile-time-constant

And in both of your examples, no, they are not compile time constants.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bruno Sant Ana
Greenhorn

Joined: May 17, 2012
Posts: 29
Henry, I read the text in the link that you posted and now I'm able to say why my two blocks of code don't compile. You can only use a compile time constant in the case argument. Therefore those two lines cause an error at compile time:



Just to clarify more this text for others, to be a variable that is a compile time constant, the variable needs to be...

declared as final
have a primitive or String type
initialized (on the same line as the declaration)
assigned to a compile time constant expression


Thank you very much Henry.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
For completeness, the discussion Henry links to is about constant expressions, but does not specifically cover case statements, which are the question here. And for case statements, the value in each case must be either a compile-time constant expression, or an enum constant. That was added in Java 5.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8404
    
  23

Mike Simmons wrote:For completeness, the discussion Henry links to is about constant expressions, but does not specifically cover case statements, which are the question here. And for case statements, the value in each case must be either a compile-time constant expression, or an enum constant. That was added in Java 5.

And as of release 7, I believe you can now use String literals.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Don’t those Strings have to be compile-time constants, too? Remember a switch-case is always set up at compile time.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Yes, the Strings have to be compile-time constants, too. Prior to 7, Strings weren't allowed as case targets, even if they were constants. Now they are allowed, but only if they are constants.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constant inside the switch case