• Post Reply Bookmark Topic Watch Topic
  • New Topic

case constants  RSS feed

 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, if I have the following finals,


private final boolean DVORAK = true;

private final int LEFT = KeyEvent.VK_A;
private final int RIGHT = DVORAK ? KeyEvent.VK_E : KeyEvent.VK_D;
private final int UP = DVORAK ? KeyEvent.VK_COMMA: KeyEvent.VK_W;
private final int DOWN = DVORAK ? KeyEvent.VK_O : KeyEvent.VK_S;

I can use RIGHT, UP, and DOWN in switches.

However, if DVORAK is a protected final boolean in a super class, they don't register as constant, even if I define them in the constructer.
It's strange that they get counted as finals, but can't be used in switches.

A) Whyzat?
B) Is the only solution (if I want DVORAK to be defined in one and only one place) to have a big else-if mess?

Thanks.
[ January 13, 2006: Message edited by: Nick George ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a thought: does the compiler replace case expressions with the value they represent? That would then make sense that it doesn't work.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that's it.

Static does the trick.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what you've said, I doubt it had anything to do with whether the field was static, or protected. More likely the problem was that it wasn't being initialized (a) right when it was declared, and (b) using a compile-time constant expression. Case arguments do indeed need to be compile-time constants. An immutable value which is only assigned at runtime is no good.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right, that's what I meant to be saying. However, if DVORAK is a static constant elsewhere, these work. I guess it figures out what it is and then compiles it as either value.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to use a boolean value as a case label? In fact, I am willing to bet this is the issue rather than the location. Of course, it sounds like you can use a private static boolean variable as a case label within the same class. But wouldn't an if statement be sufficient since there are only two possible cases here (true or false)? Typically a switch statement is useful when you have multiple (more than 3) options.

Layne
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I didn't notice DVORAK was boolean. You can't use a boolean as a case argument, period. So Nick, I'm not sure what you're talking about when you say "if DVORAK is a static constant elsewhere, these work". Are talking about using DVORAK as a case arguement, or something else? Has DVORAK ceased being a boolean at some point along the way?
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DVORAK is not the switch constant.

UP, DOWN, LEFT, and RIGHT are the switch constants, and are ints dependant on the boolean DVORAK.

If DVORAK is a final static defined elsewhere, and LEFT is defined with DVORAK, it is legit in a switch.

If DVORAK is a protected final non-static member of my superclass, and LEFT is defined with it, it is illegit in a switch.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that makes more sense now that we've got some code in the first post.

[Nick]: If DVORAK is a protected final non-static member of my superclass, and LEFT is defined with it, it is illegit in a switch.

I don't think so. At least, whether or not it's static should be irrelevant.


This compiles just fine, with DVORAK a protected final non-static member of the superclass Foo. The key is, it's also initialized with a compile-time constant when it's declared. I'm guessing that whatever your other code did that made LEFT and the others invalid case arguments, you probably didn't initialize it with a constant when it's declared.
[ January 13, 2006: Message edited by: Jim Yingst ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've bungled with it enough that I could be wrong, but I know I started with DVORAK = true;

At any rate, I've bungled it into something that works the way I want it to, and my intellectual curiosity is insufficient to determine just where I was messing up.

Thanks for the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!