I am developing for mobile on Java 1.3 and don’t have enum type, so instead am using the Typesafe Enum Pattern. E.g.
public class DownloadEvent {
//Download events
public static final DownloadEvent DOWNLOAD_STARTED = new DownloadEvent("Download started");
public static final DownloadEvent DOWNLOAD_COMPLETED = new DownloadEvent("Download completed");
private String eventDescription;
private DownloadEvent(String eventDescription){
this.eventDescription = eventDescription;
}
public String toString(){
return eventDescription;
}
}
The problem with this approach is that the compiler won’t allow classes inside switch statements e.g.
DownloadEvent event = getDownloadEvent(); //returns a download event
switch(event){
case DownloadEvent.DOWNLOAD_STARTED:
//do some stuff
}
Is there any way around this? Or should I go back to using lists of int constants
You could give the
DownloadEventa type-property (for example an int) which you can retrieve with a getter, and use that property in your switch statementBut this is of course not as-nice as using an enum