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;
}
}
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:
Firstly, because
String
is immutable, yourFoo
doesn't really have any depth, so the difference between a deep copy and a shallow copy doesn't really exist. For non-immutable type fields, settingthis.field = other.field
will makethis
a shallow copy. You will need to make a deep copy ofother.field
(recursively until you reach objects with only immutable or primitive fields).Secondly, to answer the question in the comment in you code, it works correctly for me.
System.out.println(object2.getName1());
does outputqwertyuiop
.