• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constant inside the switch case

 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21415
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10505
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic