Java Enumerations in MIDP 2 Mobile Application

I just returned to developing MIDP after 4 years of .NET 2 and Java 5 and 6. During this time, I really enjoyed using enumerations.

Enum is a language function that allows the developer to trust more parts of his code, especially in order to avoid or detect errors earlier (at compile time). Some other benefits can be found here: http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

It was strange to me that I could not find them in MIDP 2.0. I have an error message:

The "enum" type should not be used as an identifier, since it is reserved for a keyword from source level 1.5 "

I had some experience in Java 1.4 some time ago, but I didn’t remember that. Sure some features of the newer versions of your high-level languages ​​that you get for granted ...

In any case, here is a good recommendation on how to do without them (if you are developing MIDP or dealing with code prior to Java 5): http://www.javacamp.org/designPattern/enum. HTML

To summarize (more detailed information and a good explanation, follow the previous link. Many thanks to the original author):

//The typesafe enum pattern public class Suit { private final String name; public static final Suit CLUBS =new Suit("clubs"); public static final Suit DIAMONDS =new Suit("diamonds"); public static final Suit HEARTS =new Suit("hearts"); public static final Suit SPADES =new Suit("spades"); private Suit(String name){ this.name =name; } public String toString(){ return name; } } 

Do you have any other approaches to this problem?

+4
source share
1 answer

The problem with MIDP is that it is stuck at level 1.2 of the Java language (some say it is 1.3 or 1.4, but it is not), and Enums were introduced in 1.5. Your scheme is a step in the right direction, but some functions of "real" enumerations are missing, for example, assigning an ordinal number to each constant.

You may encounter similar problems with generics, annotations, etc., which were also introduced in 1.5. There are tools to convert Java 1.5 to 1.2, some of which are listed here . Thus, you should be able to encode in 1.5 and work on MIDP. Please note, however, that using these tools will greatly complicate the build process, while the solution you mentioned does not work.

+3
source

All Articles