How to write a copy constructor in Java - 037

preview_player
Показать описание
What if we want to create an instance that's just like the one over there? We just want a copy. This video looks at the copy constructor in Java, and how to create new identical instances.

A copy constructor is this. It's a method that takes an instance of a class, and returns a new instance copied exactly like the first class. You can use a static factory method, but best practice is use a constructor that takes a single instance of the class as a parameter. We'll create our copy constructor to use a constructor.

What goes in our copy constructor? Well the next thought is to create a new enemy object, and copy the attributes over. That looks like this. Unfortunately this doesn't work either. We wanted two enemies, but we really got two enemies sharing a weapon and toolbelt. If we change a value on one weapon, say to overheating, both enemies will experience the change. That's because we did something called a shallow copy.

#java #programming #tutorial





Concepts: Java, classes, java copy constructor, constructors

Social Links: Don't hesitate to contact me if you have any further questions.

Related Videos:

What are static factory methods in Java? - 036

Free Java Course Online

Media credits: All images are owned by DJ Spiess unless listed below:

Clipart

Music
Рекомендации по теме
Комментарии
Автор

The best video/tutorial in details ....I have been on youtube since 2009, never Witnessed a great way of explanation and deep efforts into the video and examples as yours! Big up! keep the great work Professor !!

BornFail
Автор

I'm new to all languages. I enjoy your videos--they're short and simple.

janeanallen
Автор

I may have missed something here, but how are we able to return newNinja if the method isn't a return type method?

jameswhyte
Автор

Did you design the enemy? Yes, copying always comes with problems. I totally agree. When I was programming - oh, so many years ago. I actually would retype things if I felt led to copy. That way, I was mentally checking it to see if it was something I needed to update. So tricky. Great video. I'm itching for the time to work my way through your videos. I hope you are doing well!

SecondsWithMCOfficial
Автор

for one of my project I needed to copy very complex object. I did it using serialization. simply serialize and then deserialize the object. I also found that there are some API that will copy object but that also using serialization technique. But it may be impact on application performance. But it worked for me in a easy way.

MuztabaHasanat
Автор

Great video exactly what i was looking for :D thanks

youssofism
Автор

Damn, you'd think after decades Java would already have a solution to this

pleinair
Автор

Such great videos and teacher ! . Thanks for the efforts.

junaidtanoli
Автор

Boss: Perform a deep copy
Rookie: how deep?
Boss: balls deep son

rehanayoub
Автор

Constructors cannot have even void as return type and you're returning a new Instance in the copy constructor.. how's that possible?
Isn't it should be like this:

public Ninja(Ninja copy)
{
name=copy.name;
}

//main()
Ninja ninja=new Ninja("george");
Ninja ninjacopy=new Ninja(ninja);

please do correct me if I'm wrong...

And can you explain how primitive data types('int' in this case) are immutable because we don't really have the reference to the variables we assign?

amitrawat
Автор

Thank you soooo much!! I now have a better understanding of this concept :)!

winfredamazvidza
Автор

suppose I have below situation where I have reference type in class foo.
Example -
public class Foo {
private int a;
private Map<String, Bar> map;
private List<Double> list;
}

public class Bar {
int d;
double e;
}
I have to create a deep copy of objects of Foo class. So I am confused in copy constructor of class Foo. Do I have to create one new instance of Map, then use copy constructor of Bar to create new Bars then assign them to the Map ?
something like
(for class Foo)
public Foo copy(Foo oldFoo)
Foo newFoo = new Foo();
newFoo .a = oldFoo.a;
Map<String, Bar> newMap = new HashMap<String, Bar>();
for(Entry e : map.entryset)
{
String key = e.getKey();
Bar oldBar = e.getValue();
Bar newBar = new Bar(oldBar); // invoke copy constructor of Bar
newMap.put(Key, newBar) ;
}
newFoo .map = newMap;

//same for list
return newFoo
}
does it seem right ??

ohefnsz
Автор

sorry I believe you have a mistake, we cannot return in constructors.

KY-xzyb
Автор

I am a little confused on why the constructor returned something?

VampireBloodedEasonC