aspose file tools*
The moose likes Beginning Java and the fly likes Case constant must be compile time constant? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Case constant must be compile time constant?" Watch "Case constant must be compile time constant?" New topic
Author

Case constant must be compile time constant?

asha ganapathy
Ranch Hand

Joined: Nov 03, 2006
Posts: 54
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

Joined: Nov 18, 2006
Posts: 689

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

Joined: Nov 03, 2006
Posts: 54
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
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!


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Red Smith
Ranch Hand

Joined: Aug 05, 2007
Posts: 136
    
    1
Originally posted by Ilja Preuss:


Because b is not a compile time constant.


What not? It is declared as final.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thats really a perfect and wonderful explanation Jim.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Sridhar Padala
Greenhorn

Joined: Aug 03, 2007
Posts: 20
Thank You Very Much.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Case constant must be compile time constant?