2

This is my current enum class:

public enum BuildingType {
MINER("Miner", "A basic slave." (*, Miner (separate class)*)), FARM("Farm", "Old Macdonald's Crib."), BAKERY("Bakery", "Best Cookies in Town!"), FACTORY("Factory", "Highest Quality Memes in town!"), QUARRY("Quarry", "Let's get drilling!");

private String name;
private String description;
//private Class clazz;

BuildingType(String name, String description (* Possible to put Class clazz reference here? *)) {
    this.name = name;
    this.description = description;
    //this.clazz = clazz
}

public String getName() {
    return name;
}

public String getDescription() {
    return description;
}

public Class getReferencedClass() {
    //Return referenced "clazz" above
}
}

As put in the comments and (* *), is it possible to reference a static class inside an enum so I can change static values inside that class by just referencing building type?

e.g.

BuildingType.MINER.getReferencedClass.setCookiesPerSecond(4);

Thanks all,
Jaco :)

2 Answers 2

2

You can do this using reflection, however it would be much better to have an instance for that class which implements an interface that all the other implementations implement even if there is only even one instance of that interface e.g.

enum StaticData implement IData {
    INSTANCE; // only one instance

    /* put your non static fields here */

    /* put your methods for IData here */
}

public enum BuildingType {
    MINER("Miner", "A basic slave.", StaticData.INSTANCE), FARM("Farm", "Old Macdonald's Crib."), BAKERY("Bakery", "Best Cookies in Town!"), FACTORY("Factory", "Highest Quality Memes in town!"), QUARRY("Quarry", "Let's get drilling!");

// later
BuildingType.MINER.getIData().setCookiesPerSecond(4);
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you can reference classes by their name followed by .class idiom. To show it in you example:

public enum BuildingType {
    MINER("Miner", "A basic slave.", Miner.class), FARM("Farm", "Old Macdonald's Crib.", Farm.class),
    BAKERY("Bakery", "Best Cookies in Town!", Bakery.class),
    FACTORY("Factory", "Highest Quality Memes in town!", Factory.class),
    QUARRY("Quarry", "Let's get drilling!", Quarry.class);

    private String name;
    private String description;
    private Class<?> clazz;

    BuildingType(String name, String description, Class<?> clazz) {
        this.name = name;
        this.description = description;
        this.clazz = clazz;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public Class<?> getReferencedClass() {
        return clazz;
    }

    public static class Miner {}

    public static class Farm {}

    public static class Bakery {}

    public static class Factory {}

    public static class Quarry {}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.