Adam B Adam B - 1 year ago 43
Java Question

Use of Enum when members are the same as another class's public variables

I have a situation where I feel like I'm doing something wrong, and I suspect there's a MUCH more efficient way to do things.

I have a class with some variables:

public class Structure {
public float size;
public float pos_x;
public float pos_y;
public float pos_z;

And I have an enum with these members which are exactly the same as the above fields:

public enum StructureProperty {

I have AnotherClass that needs to store a single StructureProperty, and its associated value.

public class AnotherClass {
public StructureProperty property;
public float value;
public float ID;
public float parentID;
..some methods and things

I have a method that grabs this other class, and looks to see which property to update using the enum:

void UpdateStructure (AnotherClass anotherClassInstance) {
switch ( {

case StructureProperty.SIZE:
Structure.size += anotherClassInstance.value;

... for all other cases
but the logic might be different
(not always +=, sometimes several lines)


This method needs to check to see which property to update in the structure, and then update it. I feel like I might be breaking some kind of object oriented approach here since I need to maintain both a list of enums and a list of variables in Structure. Note that I foresee frequently adding and removing properties as I build my program. Hopefully someone can steer me in the right way of making this code more future-proof.

I'm programming in c#, but I think this question applies to all major OO programming languages.


Answer Source

I am answering this from a Java standpoint. I am sure c# will have similar constructs. I don't see any necessity of maintaining a Structure class and a StructureProperty enum. Assuming these properties need to be modified at runtime, and there is no use case of storing them in a properties file, I think Structure and StructureProperty can be consolidated into one Enum.

Here's how you could think in that direction :

public class StructurePropertyExample {

private static enum StructureProperty {

    SIZE(0.0f), POS_X(0.0f), POS_Y(0.0f), POS_Z(0.0f);

    private float value;

    private StructureProperty(float value) {
        this.value = value;

    public float getValue() {
        return value;

    public void setValue(float value) {
        this.value = value;

public static void main(String[] args) {

    float newValue = 1.23f;

    for (StructureProperty property : StructureProperty.values()) {
        switch (property) {
            case SIZE: property.setValue(newValue);
            case POS_X : // similar as above
            case POS_Y :
            case POS_Z :