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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:22:41+00:00 2026-06-04T16:22:41+00:00

I ran across this problem, which has been driving me nuts. In a nutshell,

  • 0

I ran across this problem, which has been driving me nuts. In a nutshell, I instantiate two objects of the same class. When I run a method in one object, the other object is affected too as if I called a method on a 2nd object explicitly. I was wondering if someone could please give me a hand on this.

Suppose, I have class Portfolio…

public class Portfolio implements Cloneable {

public ArrayList<Instrument> portfolio;
private String name;
private String description;

public Portfolio() {
    portfolio = new ArrayList<Instrument>();
}

public Portfolio(Portfolio copyFrom) {
    //attempt to copy the object by value
    this.portfolio = (ArrayList<Instrument>) copyFrom.portfolio.clone();
    this.name = copyFrom.name;
    this.description = copyFrom.description;
}

public void applyStress(Stress stress) {
    this.portfolio.set(0,this.portfolio.get(0)+1;
}

1st constructor is used to instantiate an object, etc. 2nd constructor is used to copy an object by value.

A method applyStress is used to run through sum calculations. in our case I simplified the method, so that it does nothing but adds +1 to whatever is in the object.

So I would instantiate an object as

Portfolio p = new Portfolio();

then I would assign to a portfolio field, some instruments;

p.portfolio = someInstrumentList;

then I would copy by value the portfolio p into pCopy:

Portfolio pCopy = new Portfolio(p);

So at this time I am have two objects that are the same. Further one is a copy-by-value object. Changing values of fields in pCopy does not affect same fields in p.

Now, when I run a method applyStress on p, then the values of the instrument list in pCopy will also change.

In other words, if p.portfolio.get(0) == 1, then after p.applyStress, I would expect to see that p.portfolio.get(0) is 2 and pCopy.portfolio.get(0) is 1

But what I see instead is p.portfolio.get(0) is 2 and pCopy.portfolio.get(0) is also 2

I do not understand why this happens. It is not the static modifier issue, as there is no static modifiers. Anyone has any ideas?

  • 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-04T16:22:43+00:00Added an answer on June 4, 2026 at 4:22 pm

    The clone method applied to you your ArrayList reference does a shallow copy, not a deep copy. This implies that whatever you had in the original collection is shared by the cloned one.

    This means that you need to clone every instrument as well, or provide a copy constructor for every one of them.

    this.portfolio = new ArrayList<Instrument>();
    for(Instrument toBeCopiedInstrument : copyFrom.portfolio){
       this.portfolio.add(new Instrument(toBeCopiedInstrument ));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently ran across this problem while trying to implement a service that has
I ran across this code recently which is part of a template class: operator
In answering this code golf question , I ran across a problem in my
While just playing with jQuery/JavaScript I ran across this problem. I could capture Alt
Interesting problem I ran across which makes total sense. I have a generic method
Hii , I am developing an application where I ran across into this problem
In my CS homework for Computer Architecture, I ran across this interesting problem. My
I ran across this snippet in an internal web site, but I'm having trouble
I ran across this article on copy ellision in C++ and I've seen comments
I ran across this chunk of code (modified) in our application, and am confused

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.