Hydroxocobalamin
Hydroxocobalamin

Reputation: 79

How can I store different classes in an enum?

I have an enum, EnumUtil, and a few classes, ChatUtil, PrefixUtil, ect. The ChatUtil and PrefixUtil classes extend the class Util. What I need is a way to store each of them inside the enum.

Example:

I have a class that needs the prefixes, so I'd call Enumutil.PREFIX_UTIL, and it'd return the PrefixUtil class.

Here is my code, which I don't think works:

public enum EnumUtil
{
    CHAT_UTIL(My ChatUtil classfile),
    PREFIX_UTIL(My PrefixUtil classfile);

    private final Class<? extends Util> classFile;
    private EnumUtil(Class<? extends Util> file)
    {
        this.classFile = file;
    }

    public Class get()
    {
        return this.classFile;
    }
}

I know for certain this does not work, because it always returns EnumUtil. Also, I need to mention I have no idea how to use an enum correctly. I've looked at many tutorials, but I have not found any for this sort of question.

Here is the actual code, all I did was replace "ChatUtil.class" (No actual quotation) with My ChatUtil class.

So it looks like the following:

CHAT_UTIL(ChatUtil.class), PREFIX_UTIL(Prefixes.class); Here is how I call said enum:

Prefixes prefix = EnumUtil.PREFIX_UTIL; And here is the error I recieve:

Incompatible types: EnumUtil cannot be converted to Prefixes

EDIT:

I'll just go back to the old way, by making Util handle it. It both worked and was efficient.

Thanks for the comments and suggestions!

Upvotes: 1

Views: 181

Answers (3)

Leo
Leo

Reputation: 6570

I think you want something like this.

public enum EnumUtil {
    CHAT_UTIL(ChatUtil.class),
    PREFIX_UTIL(PrefixUtil.class);

    private Class<? extends Util> clazz;
    private EnumUtil(Class<? extends Util> clazz){
        this.clazz = clazz;
    }
    public Class<? extends Util> getClazz() {
        return clazz;
    }
    public Util getUtil() throws InstantiationException, IllegalAccessException {
        return clazz.newInstance();
    }

}

and

public class Util {

}

and

public class ChatUtil extends Util {

    @Override
    public String toString() {
        return "ChatUtil [getClass()=" + getClass() + "]";
    }

}

and

public class PrefixUtil extends Util {

    @Override
    public String toString() {
        return "PrefixUtil [getClass()=" + getClass() + "]";
    }

}

and

public class Test {

    /**
     * @param args
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        System.out.println(EnumUtil.CHAT_UTIL);
        System.out.println(EnumUtil.PREFIX_UTIL);
        System.out.println(EnumUtil.CHAT_UTIL.getUtil());
        System.out.println(EnumUtil.PREFIX_UTIL.getUtil());
    }

}

Upvotes: 0

user207421
user207421

Reputation: 310866

Prefixes prefix = EnumUtil.PREFIX_UTIL;

should be

Prefixes prefix = EnumUtil.PREFIX_UTIL.get().newInstance();

But it's completely pointless. You've encoded the class name at the calling site, which is what you said you're trying to avoid. You could put the newInstance() inside the get() method, but then what will it return? It will have to return Object, so you would have to write

Prefixes prefixes = (Prefixes)EnumUtil.PREFIX_UTIL.get();

which is getting worse and worse, with the class name encoded twice at the calling site. You may as well write

Prefixes prefixes = Prefixes.class.newInstance();

or indeed

Prefixes prefixes = new Prefixes();

just like anybody else would.

Upvotes: 1

NickL
NickL

Reputation: 4276

It should work. this.classfile refers to the classfile supplied in the enum. So EnumUtil.CHAT_UTIL.get() would return 'My chatutil classfile'. However, get() would always return a Class object, which seems kind of pointless to me..

Upvotes: 0

Related Questions