Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8302551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:19:05+00:00 2026-06-08T17:19:05+00:00

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

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:19:06+00:00Added an answer on June 8, 2026 at 5:19 pm

    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>>.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that defines its own enum like this: public class Test
I have a base class which looks something like this: class Base { public:
I have a class that contains a variable of indeterminate type, which must be
I have the following class: class Enum(RootFragment): def __init__(self, column, packageName, name, modifiers=[public], enumValues=[]):
I have an enum class called Gender , and I have values in this
In java I have: public class MyClass{ public enum STATUS { ZERO, ONE ,
I have a class that is effectively a object based enum. There is a
In ASP.NET MVC 2, I have a Linq to sql class that contains a
Basically, I have a class defined in my website which contains a bunch of
I have a class representing a finite-state machine, which should run in a forever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.