Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 28
Registered: ‎03-15-2010
My Device: none

switch statement, enums and eclipse

[ Edited ]

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???!!!!!!  :smileymad:

 

 

And why the hell can't I use switch statements with other types?!

 

Contributor
Posts: 28
Registered: ‎03-15-2010
My Device: none

Re: switch statement, enums and eclipse

[ Edited ]

Anyone?!  :smileymad:

Developer
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.997 Dev OS, Z30 (STA100-5)-10.3.1.997 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: switch statement, enums and eclipse

Because it's Java. Java only allows integer types for switch statements.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: switch statement, enums and eclipse

[ Edited ]

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.)




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.