Is it possible to use Enum in Android like this?
public enum EventAction
{
SDCARD_MOUNTED
{
public String toString() {
return "External SDCard was mounted";
}
public int getCode() {
return 25;
}
}
}
From the outside code I have an access only to EventAction.SDCARD_MOUNTED.toString() but .getCode() is not visible. Examples I saw show how getCode() is used from inside code.
Declare getCode()
as an abstract
method:
public enum EventAction
{
SDCARD_MOUNTED
{
@Override
public String toString() {
return "External SDCard was mounted";
}
@Override
public int getCode() {
return 25;
}
};
public abstract int getCode();
}
If every value is going to be implemented the same way, it's clearer to do this:
public enum EventAction {
SDCARD_MOUNTED(25, "External SDCard was mounted");
private final int code;
private final String message;
private EventAction(int code, String message) {
this.code = code;
this.message = message;
}
@Override
public String toString() { return message; }
public int getCode() { return code; }
}
You need declare the method at the enum level :
public enum EventAction
{
SDCARD_MOUNTED
{
public String toString() {
return "External SDCard was mounted";
}
public int getCode() {
return 25;
}
};
public abstract int getCode();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With