This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
Can someone explain why we used (Car x) as an argument of the copy method? What does this x mean? Is it a placeholder for whatever name we're gonna give to the object? Which object does it refer to?
X is the object of type Car that you'll be copying the attributes from. (This)aka car 2 is doing the calling, so u place the attributes from X onto car 2. A copy constructor basically a method that copies the attributes of an object onto another object of the same type. X in this case is Car1.
Instances of classes, wether it's either predefined classes in java like String or classes created by programmers, all of them are stored in memory(RAM), this is called data by reference. In the tutorial, copying objects means to do an object point at the same memory space by the reference. Pay attention when the teacher equalize car2=car2 and print both object return the same memory address. I expect that you understand me, I'm not native spiker and have a low level of English.
Hello, this video really helped me, but I have a question. Can I accses the values of Car x in the copy method directly like this since the method is in the Car class: public void copy(Car x) { this.setMake(x.make); this.setModel(x.model); this.setYear(x.year); } it worked for me, but i am rather asking. btw I watched almost the entire java playlist (most of it in the 12h. video) and I love the way you're doing it, it's amazing. (sorry for my English)
You can do that and it will work fine if it is set up in the same way. However, it is best practice to make member variables private and use public/protected getters and setters for them. The reason being is let's say you don't want specific things to be set as the value stored in the variable, you can stop this by handling this in your setter however you would like. Then, the only way you can retrieve that variable is by using the defined getter method (since the member variable has private access). If you're calling the variables from within the class itself, it isn't required that you use a getter and setter (since private access doesn't matter within the same class), but, again, it is best practice to make getters and setters to handle how code is accessed and updated. Hope this helps!
//***********************************************
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Chevrolet","Camaro",2021);
//Car car2 = new Car("Ford","Mustang",2022);
//car2.copy(car1);
Car car2 = new Car(car1);
System.out.println(car1);
System.out.println(car2);
System.out.println();
System.out.println(car1.getMake());
System.out.println(car1.getModel());
System.out.println(car1.getYear());
System.out.println();
System.out.println(car2.getMake());
System.out.println(car2.getModel());
System.out.println(car2.getYear());
}
}
//***********************************************
public class Car {
private String make;
private String model;
private int year;
Car(String make,String model,int year){
this.setMake(make);
this.setModel(model);
this.setYear(year);
}
Car(Car x){
this.copy(x);
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public void setYear(int year) {
this.year = year;
}
public void copy(Car x) {
this.setMake(x.getMake());
this.setModel(x.getModel());
this.setYear(x.getYear());
}
}
//***********************************************
This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
great video!!! I'm infinitely grateful for your dedication and big heart to share this knowledge with the world. Thank you soo much
you really got style. you are changing lives Bro. I thank you from the bottom of my heart.
you're my dude buddy. nice voice-over, excellent video quality, and easy-to-follow content. God bless you.
Really helpful for someone who is learning Java in university thank you my man!
aweeeeeeesome bro..this is the best course in whole youtube
Absolutely easier after you explain everything. Thank you Bro! you save my day
Great explanation! Keep on the good work!
thanks for watching Yahel!
wow great explanation
Thanks for the clear explanation.
Superb explanation bro code
Can someone explain why we used (Car x) as an argument of the copy method? What does this x mean? Is it a placeholder for whatever name we're gonna give to the object? Which object does it refer to?
X is the object of type Car that you'll be copying the attributes from. (This)aka car 2 is doing the calling, so u place the attributes from X onto car 2. A copy constructor basically a method that copies the attributes of an object onto another object of the same type. X in this case is Car1.
excellent explanation, love it
Great video!!!!!!
Man, I love your videos, you are so underrated (your videos should have more views)
thx for your explanation
Thanks, excelent
nice
Awesome
you are the best
Is it good practice to always have a copy method and overload the constructor to copy as well?
not necessarily but it's a common practice
Keep up the great work!
thank you for watching Vincent!
Great Job
thanks for watching Metin!
Brilliant Brilliant explanation and exactly what I needed for my Pokemon game :)
Nice.
Thank you so much for your effort
Amazing video! I was looking for copying objects for so long.... thanks bro
brilliant
Thank you for this tutorial, Bro!
Love it 👍
Commenting here just because Bro asked me to do so!
This is a good explanation!
Thank you Bro🤗How are you doing? This tutorial is great!
Please make videos for mobile app development....
Thank you very much!
Thanks, very helpful!
🎉
Im kinda confused at this point :/
Instances of classes, wether it's either predefined classes in java like String or classes created by programmers, all of them are stored in memory(RAM), this is called data by reference. In the tutorial, copying objects means to do an object point at the same memory space by the reference. Pay attention when the teacher equalize car2=car2 and print both object return the same memory address. I expect that you understand me, I'm not native spiker and have a low level of English.
@@yanluisnunezlara319fué tremenda explicación mi hermano, muchas gracias!
I love you bro
You are great!!
¡Muchas gracias!
Hello, this video really helped me, but I have a question. Can I accses the values of Car x in the copy method directly like this since the method is in the Car class:
public void copy(Car x) {
this.setMake(x.make);
this.setModel(x.model);
this.setYear(x.year);
}
it worked for me, but i am rather asking. btw I watched almost the entire java playlist (most of it in the 12h. video) and I love the way you're doing it, it's amazing.
(sorry for my English)
Thanks for pointing this out. It actually works and makes sense. Thanks bro!
You can do that and it will work fine if it is set up in the same way. However, it is best practice to make member variables private and use public/protected getters and setters for them. The reason being is let's say you don't want specific things to be set as the value stored in the variable, you can stop this by handling this in your setter however you would like. Then, the only way you can retrieve that variable is by using the defined getter method (since the member variable has private access). If you're calling the variables from within the class itself, it isn't required that you use a getter and setter (since private access doesn't matter within the same class), but, again, it is best practice to make getters and setters to handle how code is accessed and updated. Hope this helps!
what planet are you from .. ? 100% informative
Thanks bro
This is considered a shallow copy right?
is it possible to change a private method or variables only by getter method in different class? (don't use setter only getter)
I believe you would need to call some method within the class that contains the private member to access it and change it
@@BroCodez could you give me an example?
It s pretty easy and logic
Thanks bro! Awesome channel!
Thanks, very helpfull!
Thank you so much sir.
Thanks
what happen if we don't use this while calling copy method and set method
Thanks a lot Bro! 🙌
But I had no idea how to do it in non-encapsulated classes :/
Oh, finally figured out!
//~~MAIN CLASS~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class Main {
public static void main(String[] args) {
Weapon weapon1 = new Weapon("Sword", 10);
Weapon weapon2 = new Weapon("Dagger", 5);
System.out.println("WEAPON 1:");
System.out.println(weapon1.name);
System.out.println(weapon1.damage);
System.out.println();
System.out.println("WEAPON 2:");
System.out.println(weapon2.name);
System.out.println(weapon2.damage);
System.out.println();
//Copying
weapon2.copy(weapon1);
System.out.println("New WEAPON 2:");
System.out.println(weapon2.name);
System.out.println(weapon2.damage);
}
}
//~~WEAPON CLASS~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class Weapon {
//ATTRIBUTES
String name;
int damage;
//CONSTRUCTORS
Weapon(String name, int damage){
this.name = name;
this.damage = damage;
}
//METHODS
void copy(Weapon x) {
this.name = x.name;
this.damage = x.damage;
}
}
[OUTPUT]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WEAPON 1:
Sword
10
WEAPON 2:
Dagger
5
New WEAPON 2:
Sword
10
Thank You So Much
Bro, how do I make a deep copy of an array?
Thanks, Bro! ☕ You're awesome!
thanks
Merci Bro
Thanks, Bro!
thankyou
thank you my Bro ♥♥♥
Bro ? will you be making a video for cloning in java ?
I'm not sure, it depends if I can fit it into this playlist
commenting for algo, great video
Bro code you are a fucking legend!
Thanks bro!
amazing, thank you!
Thanks bro!!
czx
where is the new videos?!
now i'm a copy machine
amazing thats what i can say
let me just copy this
Need rewatch
Ai made it easy copy code
comment
excellent explanation, love it
nice
thanks bro
Thanks
Thanks, Bro!
thanks
comment
thanks