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;
I’m not entirely sure what your line of thought is here.
If you call
changeState(NOT_ACTIVE)on some instance ofSomeObjects, then as per the definition of the method it will set that object’sstatefield to beNOT_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 you want the
changeStatemethod to apply the state to all of the objects contained within theobjectsfield, then you can make it do this with a simple loop:Again it isn’t clear to me exactly what you’re thinking of here. Don’t you want to set the
statevalue of all theSomeObjectinstances contained within aSomeObjectsinstance? 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
objectscollection to have exactly the same state at all times, it is probably wrong for them to all have an individualstatefield. It’s a modelling error, in that aSomeObjectinstance 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
SomeObjecthaving astatefield, it should have agetState()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
SomeObjectsinstance (that’s not a particularly informative class name). So it might be implemented as following:This is consistent with how I understand your design – each
SomeObjecthas the same state as its wrappingSomeObjects(and so implicitly must have exactly one parentSomeObjectsinstance 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
intorStringorMap<MyFoo, Set<Future<ResultSet>>.)