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