Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extends Enum in Java?

Tags:

java

enums

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

like image 897
Devu Soman Avatar asked Dec 13 '12 09:12

Devu Soman


People also ask

Can I extend an enum Java?

No, we cannot extend an enum in Java. Java enums can extend java. lang. Enum class implicitly, so enum types cannot extend another class.

Can you modify an enum in Java?

Enum constants are implicitly static and final and you can not change their value once created. Enum in Java provides type-safety and can be used inside switch statements like int variables.

Can you get length of enum?

To get the length of an enum: Use the Object. keys() method to get an array containing the enum's keys. Access the length property on the array.


2 Answers

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;
}
like image 103
thaussma Avatar answered Oct 03 '22 16:10

thaussma


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.

like image 45
atamanroman Avatar answered Oct 03 '22 15:10

atamanroman