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.
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!
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.
//***********************************************
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
you really got style. you are changing lives Bro. I thank you from the bottom of my heart.
great video!!! I'm infinitely grateful for your dedication and big heart to share this knowledge with the world. Thank you soo much
you're my dude buddy. nice voice-over, excellent video quality, and easy-to-follow content. God bless you.
Absolutely easier after you explain everything. Thank you Bro! you save my day
aweeeeeeesome bro..this is the best course in whole youtube
Great explanation! Keep on the good work!
thanks for watching Yahel!
excellent explanation, love it
Man, I love your videos, you are so underrated (your videos should have more views)
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.
wow great explanation
Thanks for the clear explanation.
Superb explanation bro code
Keep up the great work!
thank you for watching Vincent!
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
thx for your explanation
Thank you for this tutorial, Bro!
brilliant
Thanks, excelent
Thank you very much!
you are the best
Thank you so much for your effort
Brilliant Brilliant explanation and exactly what I needed for my Pokemon game :)
Amazing video! I was looking for copying objects for so long.... thanks bro
Please make videos for mobile app development....
Thank you Bro🤗How are you doing? This tutorial is great!
This is a good explanation!
Nice.
thanks bro
Commenting here just because Bro asked me to do so!
Awesome
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!
Thanks, very helpful!
nice
¡Muchas gracias!
You are great!!
Love it 👍
Thanks, Bro!
Thanks
This is considered a shallow copy right?
Great Job
thanks for watching Metin!
what planet are you from .. ? 100% informative
Thanks, very helpfull!
thanks
It s pretty easy and logic
thankyou
Thanks bro! Awesome channel!
Thank you so much sir.
Thank You So Much
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?
Thanks, Bro! ☕ You're awesome!
I love you bro
Thanks bro!
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
Bro code you are a fucking legend!
amazing, thank you!
🎉
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
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!
Thanks bro!!
thank you my Bro ♥♥♥
Merci Bro
Bro, how do I make a deep copy of an array?
what happen if we don't use this while calling copy method and set method
czx
now i'm a copy machine
where is the new videos?!
amazing thats what i can say
let me just copy this
Need rewatch
Ai made it easy copy code
comment
excellent explanation, love it
Thanks
thanks
nice
Thanks, Bro!
Thanks bro
comment
thanks