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