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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:58:43+00:00 2026-06-11T19:58:43+00:00

I have a main class called Bar that calls the class Foo, and I

  • 0

I have a main class called Bar that calls the class Foo, and I think I put in a deep constructor correctly

The purpose of a deep copy constructor is to copy the contents of one object to another object and changing the copied object shouldn’t change the contents of the original, correct?

My code does that, but I don’t understand why when I set the original object variable, the copying object doesn’t contain that set variable, it just contains the default constructor variable.

public class Bar
{
    public static void main(String[] args)
    {
        Foo object = new Foo();
        object.setName1("qwertyuiop");



//the below line of code should copy object to object2?
        Foo object2 = new Foo(object);          
        System.out.println(object.getName1());

//shouldn't the below line of code should output qwertyuiop since object2 is a copy of object? Why is it outputting the default constructor value Hello World?
        System.out.println(object2.getName1()); 

//changes object2's name1 var to test if it changed object's var. it didn't, so my deep copy constructor is working

        object2.setName1("TROLL");
        System.out.println(object2.getName1()); 
        System.out.println(object.getName1());
    }


}

public class Foo
{

    //instance variable(s)
    private String name1;

    public Foo()
    {
        System.out.println("Default Constructor called");
        name1= "Hello World";

    }
    //deep copy constructor
    public Foo(Foo deepCopyObject)
    {   

        name1 = deepCopyObject.name1; 

    }
    public String getName1() {
    return name1;
}
public void setName1(String name1) {
    this.name1 = name1;
}
}
  • 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-11T19:58:44+00:00Added an answer on June 11, 2026 at 7:58 pm

    That’s not deep copy. Java is not C++. You are free to write a copy constructor that takes a Foo instance and initializes it with another Foo, but there’s no language support to help you with the implementation. It’s completely up to you.

    You also should know that Java doesn’t require a copy constructor the same way C++ does. Java objects live on the heap. The thing you pass to a method is a reference to an object on the heap, not a copy of an object.

    You can write a copy constructor, but it’s up to you how it acts. You’ve got to be very careful:

    public class Foo {
        private Map<String, Bar> barDictionary;
    
        public Foo() {
            this.barDictionary = new HashMap<String, Bar>();
        }
    
        public Foo(Foo f) { 
            // What happens here is up to you.  I'd recommend making a deep copy in this case.
            this.barDictionary = new HashMap<String, Bar>(); 
            this.barDictionary.putAll(f.barDictionary);  // Question: What about the Bar references?  What happens to those?
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 assemblies, A containing the Main method and the Foo class, that
I have a class called Foo which has a member that is a pointer
I have a main class and two extended classes: class Main { public $foo;
I have a main class in a program that launches another class that handles
So I have a main class and another class that has a variable I
I have a Class(call it main class) and the method in main class calls
I have a main window class built in Qt Designer and called Ui_MainWindow which
I have class Foo class Bar Now, I want Foo* Foo::singleton = new Foo();
Here is the setup: I have a class called Sprite that contains an image
I have three classes organized in following manner. Foo is a template class, Bar

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.