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

Switch statement and constant variables

frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
The Switch statement requires constants in the case labels. But since constants don't have to be initialized at declaration why can't those case label variables be initialized at runtime?
I'm trying to prove this point and can't get this to compile :
public class Switchtest
{
public static void main(String[] args)
{
int test = 0;
final int x;

x = 1;

switch (test)
{
case x: System.out.println("case x " );break;
default: System.out.println("Default Switchtest ");break;
}
}
}
It gives an error about expecting x to be constant.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Do you have a background in C or C++? The term "constant variable" in those languages don't have an exact analog in Java, so sometimes you can confuse yourself.
The case labels need to be compile time constants, that is, values known to the compiler at compile time. A final variable is not really the same as a constant, because as you are doing in your code, you have deferred giving it a value until run-time. It's true that once you assign it a value it can't change, but it's still not a "constant" the way a C "const" is.
So that prevents you from just using a final variable as a case label, unless you provide an initializer for it. If you had done this instead:

final int x = 1; //this is as close to a "const" as you get in java - but notice this includes the initializer
then the value of x would be known to the compiler at compile-time, and you would not get an error.
[ May 15, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Thanks for clariying that. None of the 6 Java books I have clearly said the variables had to have values that could be determined at compile time. Usually they equate final variables with constants and that was the source of my problem.
I put my foot in my mouth on this issue and am trying to weasal out of it using constuctors. Do you think the code below can makes my point ?

[ May 16, 2002: Message edited by: herb slocomb ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Well, at the risk of being overly technical, no, this doesn't prove your point.
You actually have two different switch statements, with two different sets of compile-time constants in each constructor. There is no way in java to use variables as case labels, although your example shows a way of possibly getting around this. But what if you had 10,000 potential permutations of possible case label values? Are you really going to code 10,000 constructors for them?
Of course what you really want here is generative programming, where at run-time, you would dynamically create java source code, and compile it, and execute it, so you could get the effect of variable case labels, while still working with java's swtich model requiring compile-time constants.
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Originally posted by Rob Ross:
Of course what you really want here is generative programming, where at run-time, you would dynamically create java source code, and compile it, and execute it, so you could get the effect of variable case labels, while still working with java's swtich model requiring compile-time constants.

Yes, that's the creative type of answer I was looking for!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch statement and constant variables