• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Case constant must be compile time constant?

 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I didnt understand what this means, "Case constant must be compile time constant". Can anyone explain me this with the example.

Thanks in Advance
:roll:
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a switch statement such as the following:



z is a variable, and you can switch on it.

CONSTANT is a constant, so you can case on it.

CONSTANT + 10 is an expression, but it is an expression that can be evaluated at compile-time so you can case on it.

On the other hand:



In the above example, x is treated as a variable at compile-time because, although it is known at compile time, it is not declared final so the compiler doesn't try to evaluate x or x + 10 so they are variables -- not constants, so you can't case on them. The above code would not compile.

Kaydell
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kaydell,

I understood that only constants can be used for case statements and not variables.But how about this code?why should this code have compiler errors?

[code]
final int a =1 ;
final int b;
b=2;
int x=0;
switch(x){
case a:
case b: //compiler error;
}
[\code]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by asha ganapathy:
Thanks Kaydell,

I understood that only constants can be used for case statements and not variables.But how about this code?why should this code have compiler errors?

[code]
final int a =1 ;
final int b;
b=2;
int x=0;
switch(x){
case a:
case b: //compiler error;
}
[\code]


Because b is not a compile time constant.
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only plus a tip, in it care with wrappers java 5 that it is not I validate for marries. Had to the resource of autoboxing.
so long!
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


Because b is not a compile time constant.


What not? It is declared as final.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order for b (or any variable) to be a compile-time constant, it is necessary but not sufficient that it be final. Additionally, it must be a primitive type or String, and it must be initialized in the same line it is declared, using another compile-time constant expression. That last is the requirement that is not met here, as b was only set to 2 one line after the declaration.

You may wonder why this is important, given that the variable b is definitely assigned using another compile-time constant (2) just one line after the declaration. The thing is, the compiler is not expected to analyze the code after a declaration to determine whether a variable is "really" a constant or not. It could be really simple code, or complex code; it doesn't matter.


In this case the code was really simple, and it may seem like the compiler "should have known" that b was a constant. But at the same time, since the code was so simple, the programmer could have simply initialized b on the same line it was declared:

If you want a variable to represent a compile-time constant, make sure it's initialized on the same line it's declared. If you can't do that, it's not a compile-time constant.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats really a perfect and wonderful explanation Jim.
 
Sridhar Padala
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Very Much.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic