Reputation: 2266
I have a class from sample project.But when I am using this class it shows some errors.The class is given below.
public class q extends Enum
{
private int i = -1;
private String s = null;
private q(String s, int i)
{
// super(s, i);
this.s = s;
this.i = i;
}
public static q valueOf(String s)
{
return (q)Enum.valueOf(q.class, s);
}
public static q[] values()
{
return (q[])a.clone();
}
public static final q ANDROID_VERSION;
public static final q APP_VERSION_CODE;
public static final q APP_VERSION_NAME;
public static final q AVAILABLE_MEM_SIZE;
private static final q a[];
static
{
APP_VERSION_CODE = new q("APP_VERSION_CODE", 1);
APP_VERSION_NAME = new q("APP_VERSION_NAME", 2);
ANDROID_VERSION = new q("ANDROID_VERSION", 6);
AVAILABLE_MEM_SIZE = new q("AVAILABLE_MEM_SIZE", 11);
q aq[] = new q[34];
aq[0] = APP_VERSION_CODE;
aq[1] = ANDROID_VERSION;
aq[2] = APP_VERSION_NAME;
aq[3] = AVAILABLE_MEM_SIZE;
a = aq;
}
}
When extending Enum it shows "The type q may not subclass Enum explicitly" error.How can i create an enum using these fields?
How can i modify this class to use like enum(ie,I want to use the default enum methods like ordinal(),valueOf(...) etc.. on this.)?
Upvotes: 5
Views: 17184
Reputation: 9886
You cannot extend from Enum
. You have to declare an Enum
class like:
public enum Q {
TYPE1, TYPE2, TYPE3;
}
And you also cannot instantiate an enum class directly. Each type of your enum class is instantiated exactly once by the virtual machine.
public class MyClass {
public enum MyEnum{
TYPE1("Name", 9,1,100000), TYPE2("Name2", 10, 1, 200000);
private final int androidVersion;
private final int appVersionCode;
private final int availableMemSize;
private final String appVersionName;
private MyEnum(String appVersionName, int androidVersion, int appVersionCode, int availableMemSize) {
this.androidVersion = androidVersion;
this.appVersionCode = appVersionCode;
this.availableMemSize = availableMemSize;
this.appVersionName = appVersionName;
}
}
MyEnum mType = MyEnum.TYPE1;
}
Upvotes: 6
Reputation: 11808
Enums basically boil down to something like this:
public Enum Q
{
TYPE1, TYPE2, TYPE3:
}
// is roughy translated to
public final class Q
{
private Q() {}
public static final Q TYPE1 = new Q();
public static final Q TYPE2 = new Q();
public static final Q TYPE3 = new Q();
}
There is more you can do, but this should explain why you can not instantiate Q
.
Upvotes: 5
Reputation: 33534
- Think of the enum
keyword as syntatic sugar. It sets up classes with normal inheritance trees for you, but will not allow you to extend java.lang.Enum
.
Eg:
public enum TEST {
ONE, TWO, THREE;
}
Upvotes: 3