• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Case constant must be compile time constant?

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats really a perfect and wonderful explanation Jim.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Very Much.....
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic