Тёмный

How to write a copy constructor in Java - 037 

Deege
Подписаться 26 тыс.
Просмотров 35 тыс.
50% 1

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

Like the video? Don't forget to subscribe! ➜ www.deegeu.com/...

Keep up to date with the newsletter! ➜ www.deegeu.com/...
Transcript and code: : www.deegeu.com/...
Concepts: Java, classes, java copy constructor, constructors
Social Links: Don't hesitate to contact me if you have any further questions.
WEBSITE : dj@deegeu.com
TWITTER : / deege
FACEBOOK: / deegeu.programming.tut...
GOOGLE+ : google.com/+Dee...
About Me: www.deegeu.com/...
Related Videos:
What are static factory methods in Java? - 036
• What are static factor...
Free Java Course Online
• What important Java Te...
Media credits: All images are owned by DJ Spiess unless listed below:
Clipart
openclipart.org/
Music
Just Nasty by Kevin MacLeod is licensed under a Creative Commons Attribution license (creativecommon...)
Source: incompetech.com...
Artist: incompetech.com/

Опубликовано:

 

12 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 29   
@pleinair6318
@pleinair6318 9 месяцев назад
Damn, you'd think after decades Java would already have a solution to this
@saimatayyab6007
@saimatayyab6007 3 года назад
Informative👍
@saimatayyab6007
@saimatayyab6007 3 года назад
😊
@saimatayyab6007
@saimatayyab6007 3 года назад
@@vlogs_by_ahmad__3995 ajaoo? Ya bht late ho gayi
@Born2Fail
@Born2Fail 7 лет назад
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 !!
@janeanallen87
@janeanallen87 9 лет назад
I'm new to all languages. I enjoy your videos--they're short and simple.
@deegeu
@deegeu 9 лет назад
+Janean Allen Thank you! I'm glad you enjoy them, and I hope they help you.
@janeanallen87
@janeanallen87 9 лет назад
+Deege U They do! I'm learning Android Studio--as you know I must understand Java, HTML and IntelJ. Do you give tutorials on HTML & IntelJ?
@deegeu
@deegeu 9 лет назад
+Janean Allen I haven't created any on those topics yet. I'll add HTML to the growing list of topics! Thanks!
@jameswhyte1340
@jameswhyte1340 8 лет назад
I may have missed something here, but how are we able to return newNinja if the method isn't a return type method?
@csbnikhil
@csbnikhil 3 года назад
You didn't. You're right about that. That's not how it's done. A constructor never returns anything. Also, the design needs to be changed if there's a need for all the attributes to be public.
@KY-xz9yb
@KY-xz9yb Год назад
sorry I believe you have a mistake, we cannot return in constructors.
@junaidtanoli1
@junaidtanoli1 8 лет назад
Such great videos and teacher ! . Thanks for the efforts.
@deegeu
@deegeu 8 лет назад
+Junaid Tanoli Thanks for watching!
@youssofism
@youssofism 8 лет назад
Great video exactly what i was looking for :D thanks
@deegeu
@deegeu 8 лет назад
+Yusuf Shoair Glad it helped! Thanks for watching!
@d3struction61
@d3struction61 7 лет назад
You are a professional.
@winfredamazvidza3132
@winfredamazvidza3132 8 лет назад
Thank you soooo much!! I now have a better understanding of this concept :)!
@deegeu
@deegeu 8 лет назад
You're very welcome!
@MuztabaHasanat
@MuztabaHasanat 8 лет назад
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.
@rehanayoub4574
@rehanayoub4574 5 лет назад
Boss: Perform a deep copy Rookie: how deep? Boss: balls deep son
@20SecondsWithMCOfficial
@20SecondsWithMCOfficial 9 лет назад
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!
@deegeu
@deegeu 9 лет назад
+20SecondsWithMC Naw, I used openclipart.org. My art skills are pretty poor. All is well here, although it feels like NYTU is getting a bit quiet...
@20SecondsWithMCOfficial
@20SecondsWithMCOfficial 9 лет назад
+Deege U Yeah, I actually asked Katya to remove me as moderator b/c while there are some awesome YT there, I haven't noticed much give and take and there are too many that I'm not interested in following (only so much time in a day - and my time available went WAY down in the last month.
@VampireBloodedEasonC
@VampireBloodedEasonC 8 лет назад
I am a little confused on why the constructor returned something?
@deegeu
@deegeu 8 лет назад
+VampireBloodedEasonC The constructor's purpose is to create an instance of the class in a useable state. The instance is what is returned from the constructor when you call "new".
@fredmihkelsoo68
@fredmihkelsoo68 6 лет назад
The constructor sample on video has one extra line (return newNinja). Constructor itself is method with void result type. Constructor as I understand should never return anything just construct when "new" is called.
@amitrawat8075
@amitrawat8075 7 лет назад
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?
@user-oh1ef8ns6z
@user-oh1ef8ns6z 7 лет назад
suppose I have below situation where I have reference type in class foo. Example - public class Foo { private int a; private Map map; private List 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 newMap = new HashMap(); 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 ??
Далее
When to use an abstract class in Java - 038
4:26
Просмотров 124 тыс.
НЕВОЗМОЖНЫЙ ЭКСПЕРИМЕНТ
00:39
Просмотров 67 тыс.
ДЖЕФ  ЕЩЕ ПОМОГАЕТ!
09:37
Просмотров 271 тыс.
Overriding equals and hashCode in Java - 039
6:30
Просмотров 82 тыс.
Java Interfaces Explained - 040
7:12
Просмотров 155 тыс.
What are static factory methods in Java? - 036
5:04
Просмотров 17 тыс.
What important Java Terms do I need to know? - 001
8:38
How to use the Builder Pattern in Java - 035
6:01
Просмотров 41 тыс.
58. copy constructor in java
6:21
Просмотров 33 тыс.
Shallow vs Deep Copy in Java
8:22
Просмотров 87 тыс.
Generics In Java - Full Simple Tutorial
17:34
Просмотров 1 млн
Java Exception Handling - 041
9:28
Просмотров 25 тыс.
Learn Java in 14 Minutes (seriously)
14:00
Просмотров 4,7 млн
НЕВОЗМОЖНЫЙ ЭКСПЕРИМЕНТ
00:39
Просмотров 67 тыс.