Java Programming Practices – How to Store Many Global Variables

javaprogramming practices

I have around 30 not-changing "objects" (the amount of them is final, no more can be added or removed). Each object has an id as well as some booleans describing what the object is and what it isn't. Now, each objects has a variable that must be changed at runtime. Most of these variables are just an integer, but some also have strings, lists, etc.

Now I'm wondering how to implement this. My current attempt is an enum with the given objects, their properties and methods to change them (for the variable I chose just object as type, to store both integers and lists). It works, but it doesn't feel like the proper, OO-way to do this. What are the alternatives? The programming language is Java, if that matters.

Here's my attempt (a bit more complicated than what I explained above):

public enum StatusInfos {

    THING_1("id0", false ,false, false, NOT_UPGRADABLE),

    THING_1_WITH_HAT("id1", true, false, false, NOT_UPGRADABLE),
    ANOTHER_THING("id2", false, false, false, NOT_UPGRADABLE),
    GREEN_THING("id3", true, false, false, NOT_UPGRADABLE),
    TALKING_DUCK("id4", true, false, false, NOT_UPGRADABLE);


    private final String id;
    private final Boolean hasAdditionalValue;
    private Double value;
    private Double additionalValue;
    private boolean needsDouble;
    private boolean needsPerCent;


    private Integer upgradeCategory;

    private Object additionalValue;

    StatusInfos(String id, Boolean hasAdditionalValue, boolean needsDouble, boolean needsPerCent, Integer upgradeCategory){
        this.id = tag;
        this.hasAdditionalValue = hasAdditionalValue;
        this.needsDouble = needsDouble;
        this.needsPerCent = needsPerCent;
        this.upgradeCategory = upgradeCategory;
    }


    public String id(){
        return id;
    }
    public Double value(){
        return value;
    }
    public void setValue(Double value){
        this.value = value;
    }
    public boolean hasAdditionalValue(){
        return hasAdditionalValue;
    }

    public Double additionalValue(){
        return additionalValue;
    }
    public void setAdditionalValue(Double newAdditionalValue){
        additionalValue = newAdditionalValue;
    }
    public boolean hasSpecialValue(){
        return false;
    }

    public Object specialValue(){
        return null;
    }
    public void setSpecialValue(Object newValue){
        return;
    }

    public boolean needsNumbersAfterComma(){
        return needsDouble;
    }

    public boolean needsPerCent(){
        return needsPerCent;
    }

    public Integer getUpgradeCategory() {
        return upgradeCategory;
    }

    public Object getAdditionalValue(){
       return additionalValue;
    }
    public void setAdditionalValue(Object additionalValue)
       this.additionalValue = additionalValue;
    }
}

Best Answer

I'd define a class containing all of the common stuff (is the list of booleans the same among these objects?) and their getters and setters, then subclass depending on the type of the changeable item within, then put them all into a container optimized for how you look up these things (id, probably).

This way, you have one global.