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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:11:54+00:00 2026-06-08T16:11:54+00:00

I have this: import java.util.ArrayList; public class MyClass { ArrayList list = new ArrayList();

  • 0

I have this:

import java.util.ArrayList;


public class MyClass {
    ArrayList list = new ArrayList();

    public MyClass(){

    }

    public MyClass(MyClass aClass){
        this.list = aClass.getList();
    }

    public void add(int number){
        list.add(number);
    }

    public void set(int number){
        list.set(0, number);
    }

    public int get(){
        return (Integer)list.get(0);
    }

    public ArrayList getList(){
        return list;
    }
}



MyClass aName = new MyClass();
aName.add(5);
System.out.println("aName: "+aName.get());

MyClass aName2 = new MyClass(aName);
System.out.println("aName2: "+aName2.get());

aName2.set(1);
System.out.println("aName2: "+aName2.get());
System.out.println("aName: "+aName.get());

This will print:
aName: 5
aName2: 5
aName2: 1
aName: 1

I don’t want my second object changing the values in my first object.
Is there any way to stop this happening but still be able to copy properties from another object?

  • 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-08T16:11:56+00:00Added an answer on June 8, 2026 at 4:11 pm

    Then don’t pass the List from the original object to the new object, in

    public MyClass(MyClass aClass){ 
        this.list = aClass.getList(); 
    } 
    

    It makes both object have a reference to the same list (there is only one list shared between both objects).

    You should do

    public MyClass(MyClass aClass){ 
        this.list = (List) aClass.getList().clone(); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code snippet import java.util.ArrayList; import java.util.List; public class AssertTest { public
Hi have one class like this import java.util.ArrayList; public class MobilePhone { private String
I have this class: package com.problemio; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;
Hello i have a problem with this aplication. import java.util.ArrayList; import java.util.List; import android.app.Activity;
i have the following action class: package com.pendulum.web; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest;
I can't figure this out. I have the following code: import java.util.ArrayList; import java.util.List;
I have this Managed bean: import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
I have this code: package com.problemio; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;
I have this code package com.cerbertek; import java.util.ArrayList; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory;

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.