3

I have a class with enum in it and class, which contains list of that objects

public enum State {
    ACTIVE,NOT_ACTIVE;
}

public class SomeObject {
    State state;        
    public SomeObject(State state) {
        this.state = state;
    }
}    

public class SomeObjects{
    State state;
    ArrayList<SomeObject> objects = new ArrayList<Main.SomeObject>();       
    public SomeObjects(int count) {
        state = State.ACTIVE;
        for (int i = 0; i < count; i++) {
            objects.add(new SomeObject(state));
        }
    }       
    public void changeState(State state) {
        this.state = state;
    }
}

And now if I use changeState(State.NOT_ACTIVE) it will change state in SomeObjects and in all Objects in list or only in SomeObjects?

If only in SomeObjects, what should I do to change state in all Objects in list with changeState()?

Is the creating of class, containing that enum, the only way in this case?

UPDATE: thanks, but in my case i need to change state of all objects with

this.state = state;
0

5 Answers 5

3

And now if I use changeState(State.NOT_ACTIVE) it will change state in SomeObjects and in all Objects in list or only in SomeObjects?

I'm not entirely sure what your line of thought is here.

If you call changeState(NOT_ACTIVE) on some instance of SomeObjects, then as per the definition of the method it will set that object's state field to be NOT_ACTIVE. There is no way that this single method call would change multiple objects, as it's performing a single assignment on a single variable.

If only in SomeObjects, what should I do to change state in all Objects in list with changeState()?

If you want the changeState method to apply the state to all of the objects contained within the objects field, then you can make it do this with a simple loop:

public void changeState(State state) {
    this.state = state;
    for (SomeObject obj : objects) {
        obj.state = state;
    }
}

Is the creating of class, containing that enum, the only way in this case?

Again it isn't clear to me exactly what you're thinking of here. Don't you want to set the state value of all the SomeObject instances contained within a SomeObjects instance? That is, set values on instances that already exist, of a class that's already defined?

If so then this can be done as above by setting the values in a loop. The fact that the field is an enum makes no difference; any primitive or reference type would behave identically here.


Edit to answer comment: Ah, it sounds like the root problem was using a field in the first place.

Given that you want all of the contents of the objects collection to have exactly the same state at all times, it is probably wrong for them to all have an individual state field. It's a modelling error, in that a SomeObject instance doesn't really independently hold/control its own state.

Something I was going to suggest earlier is that you should have implemented methods rather than accessing the fields directly - and that's something that comes to the fore now. Rather than SomeObject having a state field, it should have a getState() method. With a given instance you have a way to get its current state - but this isn't necessarily implemented as a direct object reference.

And the implementation of this method should simply be to get the state of the wrapping SomeObjects instance (that's not a particularly informative class name). So it might be implemented as following:

public class SomeObject {
    private final SomeObjects parent;

    public SomeObject(SomeObjects parent) {
        this.parent = parent;
    }

    public State getState() {
        return parent.state; // Ideally accessed via method too
    }
}

This is consistent with how I understand your design - each SomeObject has the same state as its wrapping SomeObjects (and so implicitly must have exactly one parent SomeObjects instance defined at all times). The code above is an almost exact representation of that design.

(And there's still nothing special here with enums; things would work identically if you were returning int or String or Map<MyFoo, Set<Future<ResultSet>>.)

Sign up to request clarification or add additional context in comments.

2 Comments

in C/C++ I'd simply created a reference, and in all Objects i'd stored reference to a single variable, not a ten different variables. And i don't know if there any way to do that in Java without creating an another class I want to use it in class which will contain 2400 of objects, and it'll be terrifying to store 2400 same variables and loop over 2400 objects each time i want to change state
So the point is, that I cannot use enum in my case, and I need to use class, which contains that enum, either it'll be class, that contains list of objects, or it'll be the separate class, and create a field with that class (refernce to it, actually, because I don't create a new instance). Thank you :)
2

Your code won't change the state for all the SameObjects in your objects field.

But you could do it by wrapping the State enum in another object:

public enum State
{
    ACTIVE, NOT_ACTIVE;
}

public class CurrentState
{
    private State state;

    public State getState()
    {
        return state;
    }

    public void setState(State state)
    {
        this.state = state;
    }
}

public class SomeObject {
    private CurrentState state;

    public SomeObject(CurrentState state) {
        this.state = state;
    }
}

public class SomeObjects{
    CurrentState state;
    ArrayList<SomeObject> objects = new ArrayList<SomeObject>();

    public SomeObjects(int count) {
        state = new CurrentState();
        state.setState(State.ACTIVE);
        for (int i = 0; i < count; i++) {
            objects.add(new SomeObject(state));
        }
    }

    public void changeState(State state) {
        this.state.setState(state);
    }
}

4 Comments

and there is no other way in Java, to use enum in a way, i can use a class, containing that enum?
Sorry Maksym, I don't think I undersand your question 100%. Perhaps if you could describe what you're trying to do?
I want to create class, which contains a reference to a variable, not a variable. In C/C++ I'd simply created a reference, and I wonder, if there any way to do something similar? I know, that I can do it using class, wrapping that enum, because Java stores refernces to object, not an object. But is there any way to create reference to an enum itself?
I don't think think it's possible to do it without having a class wrapping the enum.
1

To change state in all objects, you need to change state in each object explicitly:

public enum State {
        ACTIVE,NOT_ACTIVE;
    }

    public class SomeObject {
        State state;

        public SomeObject(State state) {
            this.state = state;
        }

        public void changeState(State state) {
            this.state = state;
        }
    }

    public class SomeObjects{
        State state;
        ArrayList<SomeObject> objects = new ArrayList<Main.SomeObject>();

        public SomeObjects(int count) {
            state = State.ACTIVE;
            for (int i = 0; i < count; i++) {
                objects.add(new SomeObject(state));
            }
        }

        public void changeState(State state) {
            for (int i = 0; i < objects.size(); i++) {
                objects.get(i).changeState(state);
            }
        }

    }

You can hold enum type in array list, instead of creating wrapper class for enum:

public enum State {
            ACTIVE,NOT_ACTIVE;
        }

        public class SomeObjects{
            State state;
            ArrayList<State> objects = new ArrayList<State>();

            public SomeObjects(int count) {
                state = State.ACTIVE;
                for (int i = 0; i < count; i++) {
                    objects.add(state);
                }
            }

            public void changeState(State state) {
                for (int i = 0; i < objects.size(); i++) {
                    objects.set(i, state);
                }
                this.state = state;
            }

        }

Comments

0

After only a quick glance, I don't think it will change anything other than the class variable state in the SomeObjects class. I'm not

If you want to change the status of all objects, I think you have to go through the list again. Add a method to set state in the SomeObject-class, then do something like this:

private void changeState(State givenState) {
    for(SomeObject o: objects)
    {
        o.SetState(givenState);
    }
}

Comments

0

this would be a method for your SomeObjects-class, to change all the states of your objects in defined arraylist. (please use classnames which aren't that similiar...pretty confusing!)

public void changeAllStates(State changingState) {
    for (SomeObject yourObject : this.objects) {
         yourObject.state = changingState;
    }
}

the changeState method of your SomeObjects-class will only refer to your SomeObjects.state-variable not to your SomeObjects.objects.state-variable. (haven't used any convention for showing that hierarchy..sorry!)

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.