04-16-2010 01:42 PM - edited 04-16-2010 02:44 PM
So I have this...
public class App extends UiApplication {
public static void main(String[] args) {
App app = new App();
app.enterEventDispatcher();
}
public App() {
pushScreen(new Shcreen());
}
}
//This would be my enum class thingy
final class Direction {
public static final Direction HORIZONTAL = new Direction(0);
public static final Direction VERTICAL = new Direction(1);
private int _value;
private Direction(int value) {
_value = value;
}
public int getInt() {
return this._value;
}
public Direction getDirection() {
return (_value == 0) ? HORIZONTAL : VERTICAL;
}
}
final class Shcreen extends FullScreen {
public Shcreen() {
Gradient(Direction.HORIZONTAL);
}
public void Gradient(Direction direction) {
switch (direction.getInt()) {
//stupid eclipse says case expressions must be constant expresions...
case Direction.HORIZONTAL.getInt() :
System.out.println("HORIZONTAL!!!");
break;
//same with vertical... >:(
case Direction.VERTICAL.getInt() :
System.out.println("VERTICAL!!!");
break;
default :
System.out.println("meh...");
break;
}
}
}
Eclipse warns me that case expressions must be constant expressions, even though they are and the program works well... but still, it's annoying. Any idea how to show eclipse things are ok?
Also, I wanted to have "switch(direction.getDirection())" and "case Direction.HORIZONTAL :" but apparently I can only use switch with integers and enums, which is stupid because integers make code less readable and the alternative enum can't even be made in blackberry, and the idea that I can't use my enum-like class with a switch statement unless I use integers kinda beats the purpose of the whole deal. How am I supposed to respect conventions and fellow coders if I can't create an enum and when I make one myself I can't use it anyway???!!!!!! ![]()
And why the hell can't I use switch statements with other types?!
04-16-2010 02:21 PM - edited 04-16-2010 02:40 PM
Anyone?! ![]()
04-16-2010 02:49 PM
Because it's Java. Java only allows integer types for switch statements.
04-16-2010 04:40 PM - edited 04-16-2010 04:42 PM
BlackBerry Java conforms to JDK 1.3. As you note, it does not support any Java features introduced since then, including enums or generics
Java does not consider a function call to be a compile-time constant, even if the function is guaranteed to always return a fixed value. This is not an Eclipse issue. You can always do the following in the Shcreen class:
final class Shcreen extends FullScreen {
private static final int HORIZONTAL = Direction.HORIZONTAL.getInt();
private static final int VERTICAL = Direction.VERTICAL.getInt();
// ... and later:
switch (direction.getInt()) {
case HORIZONTAL: // ...
case VERTICAL: // ...
}
}
or, perhaps better, add int constants to the Direction class:
final class Direction {
public static final int DIR_HORIZONTAL = 0;
public static final int DIR_VERTICAL = 1;
public static final Direction HORIZONTAL = new Direction(DIR_HORIZONTAL);
public static final Direction VERTICAL = new Direction(DIR_VERTICAL);
}
and use the DIR_* constants within switch statements.
If you're willing to give up on type-checking by the compiler, you can even just go back to the old style of "enum":
interface Direction {
public int HORIZONTAL = 0;
public int VERTICAL = 1;
}
and just use the symbols instead of int values (alhtough I'd probably name them DIR_*).
P.S. From what I can tell, you could define Direction.getDirection() { return self; }. It's a pretty useless function, isn't it? (It's the kind of thing that Java enums expand into, but that's what makes Java enums kind of bloated.)