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 9181383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:18:03+00:00 2026-06-17T18:18:03+00:00

this is my example object object name: OBJ object properties: String name, int age

  • 0

this is my example object
object name: OBJ
object properties: String name, int age

now i have 2 global lists

List<OBJ> lstobj = new ArrayList<OBJ>;
List<OBJ> anotherlist = new ArrayList<OBJ>;

then i added a few records in both lists like this

Name:Ben Age:5
Name:Charles Age: 6
Name:Dan Age:7

Now I needed to change Charles’ age to “10”

and so first I should find Charles in the list and get the obj from the lists

OBJ newobj = new OBJ;
for(OBJ obj : lstobj){
  if(obj.getName.equals("Charles"){
    newobj = obj;
   }
}

and now i need to set the retrieved obj’s age to 10.

newobj.setAge(10);

this action changes not just the “lstobj” but the “anotherlist” as well. How do i set the retrieved obj without affecting the two global lists?

  • 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-17T18:18:04+00:00Added an answer on June 17, 2026 at 6:18 pm

    Try using a copy constructor

    public class Person {
    
        private String name;
        private String age;
    
        public Person(String name,String age) {
            this.name = name;
            this.age = age;
        }
    
        public Person(Person person){
            this.name = person.getName();
            this.age= person.getAge();
        }
    
        public Person(){    
        }
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String toString(){
            return "[Name:"+this.name+"][Age:"+this.age+"]";
        }
    
        public static void main(String[] args) {
    
            Person person1 = new Person("Charles","20");
            Person person2 = new Person("Rob","30");
    
            List<Person> persons = new ArrayList<Person>();
            persons.add(person1);
            persons.add(person2);
    
            List<Person> morePersons = new ArrayList<Person>();
            morePersons.add(new Person(person1));
            morePersons.add(new Person(person2));
    
            Person newobj = new Person();
            for (Person obj : persons) {
                if (obj.getName().equals("Charles")) {
                    newobj = obj;
                    newobj.setAge("10");
                }
            }
    
            System.out.println(persons);
            System.out.println("======");
            System.out.println(morePersons);
    
        }
    }
    

    The output is

    run:
    [[Name:Charles][Age:10], [Name:Rob][Age:30]]
    ======
    [[Name:Charles][Age:20], [Name:Rob][Age:30]]
    BUILD SUCCESSFUL (total time: 1 second)
    

    In your case you are modifying just one object but reflects in both list.
    This is because list stores object reference, here both the list refer to the same object that you are modifying so the changes that you make is reflected in both the list.

    What i did here is that i am copying the objects details and creating a new object and its reference is different from the original object.
    Any changes you make in the old object will not change the new one, since they have different references

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

Sidebar

Related Questions

I need to obtain a Date object from this String:This is a example) M-27\nJUN-2012
I have an array list of objects and I am using this example to
For example, I have this class class Person{ private int _id = int.MinValue; private
I have an object that has some properties and methods, like so: {name: FirstName,
How can I do inheritance with this example. I'm trying to create an object
I use a config object to store configuration information. So something like this example:
I'm using SharedPreferences to persists Object fields. Take this example: class Item { private
I am investigatin this field to obtain object detection in real time. Video example:
Say I have a class ObjectA (a view controller for example), that does this
Possible Duplicate: obj-c access property dynamically I'm attempting to access an object’s properties dynamically

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.