Java copy objects 🖨️

Поделиться
HTML-код
  • Опубликовано: 22 окт 2024

Комментарии • 104

  • @BroCodez
    @BroCodez  4 года назад +43

    //***********************************************
    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());
    }

    }
    //***********************************************

  • @pavelkvasnicka6856
    @pavelkvasnicka6856 Год назад +22

    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

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 2 года назад +5

    you really got style. you are changing lives Bro. I thank you from the bottom of my heart.

  • @dianamilenaarchilacordoba4632
    @dianamilenaarchilacordoba4632 Месяц назад +2

    great video!!! I'm infinitely grateful for your dedication and big heart to share this knowledge with the world. Thank you soo much

  • @odaakeenya
    @odaakeenya 2 года назад +10

    you're my dude buddy. nice voice-over, excellent video quality, and easy-to-follow content. God bless you.

  • @mimvt
    @mimvt Год назад +2

    Absolutely easier after you explain everything. Thank you Bro! you save my day

  • @healer35
    @healer35 Год назад +2

    aweeeeeeesome bro..this is the best course in whole youtube

  • @yahelbraun5471
    @yahelbraun5471 4 года назад +12

    Great explanation! Keep on the good work!

    • @BroCodez
      @BroCodez  4 года назад +3

      thanks for watching Yahel!

  • @gentle9964
    @gentle9964 Год назад +1

    excellent explanation, love it

  • @juanestebanparraparra8531
    @juanestebanparraparra8531 2 года назад +2

    Man, I love your videos, you are so underrated (your videos should have more views)

  • @WorstDruidEU
    @WorstDruidEU 2 года назад +2

    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?

    • @mr.prince8701
      @mr.prince8701 2 года назад +1

      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.

  • @soumelee5661
    @soumelee5661 2 года назад

    wow great explanation

  • @ShermukhammadKarimov
    @ShermukhammadKarimov 6 месяцев назад

    Thanks for the clear explanation.

  • @logeshr1761
    @logeshr1761 Год назад

    Superb explanation bro code

  • @vincentchoo7471
    @vincentchoo7471 4 года назад +4

    Keep up the great work!

    • @BroCodez
      @BroCodez  4 года назад +1

      thank you for watching Vincent!

  • @cdjosh3568
    @cdjosh3568 4 года назад +2

    Is it good practice to always have a copy method and overload the constructor to copy as well?

    • @BroCodez
      @BroCodez  4 года назад +2

      not necessarily but it's a common practice

  • @kirillutsenko9306
    @kirillutsenko9306 2 года назад

    thx for your explanation

  • @elionayzuridasilveira4140
    @elionayzuridasilveira4140 5 месяцев назад

    Thank you for this tutorial, Bro!

  • @eugenezuev7349
    @eugenezuev7349 5 месяцев назад

    brilliant

  • @cristianoseixas2417
    @cristianoseixas2417 2 года назад

    Thanks, excelent

  • @曾毓哲-b1t
    @曾毓哲-b1t Год назад

    Thank you very much!

  • @nguyennhan3966
    @nguyennhan3966 Год назад

    you are the best

  • @unalysuf
    @unalysuf Год назад

    Thank you so much for your effort

  • @Ballnose
    @Ballnose 8 месяцев назад

    Brilliant Brilliant explanation and exactly what I needed for my Pokemon game :)

  • @theultimateamazing3722
    @theultimateamazing3722 Год назад

    Amazing video! I was looking for copying objects for so long.... thanks bro

  • @kaushalprasadyadav9242
    @kaushalprasadyadav9242 2 года назад

    Please make videos for mobile app development....

  • @geneolo
    @geneolo Год назад

    Thank you Bro🤗How are you doing? This tutorial is great!

  • @pasraju
    @pasraju 2 года назад

    This is a good explanation!

  • @huuloc8719
    @huuloc8719 3 года назад

    Nice.

  • @vaika8817
    @vaika8817 Год назад

    thanks bro

  • @MrCoolTester
    @MrCoolTester 6 месяцев назад +1

    Commenting here just because Bro asked me to do so!

  • @kemann3815
    @kemann3815 2 года назад

    Awesome

  • @ramok1303
    @ramok1303 2 года назад +2

    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)

    • @mohamedbenkhalifa1299
      @mohamedbenkhalifa1299 2 года назад +1

      Thanks for pointing this out. It actually works and makes sense. Thanks bro!

    • @DigitalAstronautOG
      @DigitalAstronautOG 2 года назад +5

      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!

  • @yevgenomelchenko732
    @yevgenomelchenko732 Год назад

    Thanks, very helpful!

  • @mahdib9361
    @mahdib9361 3 года назад +1

    nice

  • @francosalgado4556
    @francosalgado4556 2 года назад +1

    ¡Muchas gracias!

  • @hannakhamutouskaya2299
    @hannakhamutouskaya2299 3 года назад

    You are great!!

  • @bakaiismaiilov4732
    @bakaiismaiilov4732 4 года назад

    Love it 👍

  • @TheEvertonDias
    @TheEvertonDias Год назад

    Thanks, Bro!

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 года назад

    Thanks

  • @mohammadreza1414
    @mohammadreza1414 3 месяца назад

    This is considered a shallow copy right?

  • @alexcaesar5377
    @alexcaesar5377 4 года назад

    Great Job

    • @BroCodez
      @BroCodez  4 года назад +1

      thanks for watching Metin!

  • @ibtesamatif4418
    @ibtesamatif4418 3 года назад

    what planet are you from .. ? 100% informative

  • @tomjohansson3520
    @tomjohansson3520 2 года назад

    Thanks, very helpfull!

  • @kurosshyu2621
    @kurosshyu2621 8 месяцев назад

    thanks

  • @stefanescu1000
    @stefanescu1000 11 месяцев назад

    It s pretty easy and logic

  • @Muhammadfaisal-kd9kx
    @Muhammadfaisal-kd9kx 11 месяцев назад

    thankyou

  • @danielmilewski7659
    @danielmilewski7659 2 года назад

    Thanks bro! Awesome channel!

  • @minhtetpaing1695
    @minhtetpaing1695 3 года назад

    Thank you so much sir.

  • @suryaa5564
    @suryaa5564 3 года назад

    Thank You So Much

  • @alexcaesar5377
    @alexcaesar5377 4 года назад +1

    is it possible to change a private method or variables only by getter method in different class? (don't use setter only getter)

    • @BroCodez
      @BroCodez  4 года назад

      I believe you would need to call some method within the class that contains the private member to access it and change it

    • @alexcaesar5377
      @alexcaesar5377 4 года назад

      @@BroCodez could you give me an example?

  • @danny.3036
    @danny.3036 3 года назад

    Thanks, Bro! ☕ You're awesome!

  • @OmerFarukAY-ij2xg
    @OmerFarukAY-ij2xg 4 месяца назад

    I love you bro

  • @annexsoftworks
    @annexsoftworks 3 года назад

    Thanks bro!

  • @qpdbwvwdbqp4730
    @qpdbwvwdbqp4730 2 года назад +1

    Thanks a lot Bro! 🙌
    But I had no idea how to do it in non-encapsulated classes :/

    • @qpdbwvwdbqp4730
      @qpdbwvwdbqp4730 2 года назад

      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

  • @NikoFool
    @NikoFool 2 года назад

    Bro code you are a fucking legend!

  • @SumbaSlice
    @SumbaSlice 3 года назад

    amazing, thank you!

  • @stephanieokechi1138
    @stephanieokechi1138 4 месяца назад

    🎉

  • @bakaiismaiilov4732
    @bakaiismaiilov4732 4 года назад

    Bro ? will you be making a video for cloning in java ?

    • @BroCodez
      @BroCodez  4 года назад +3

      I'm not sure, it depends if I can fit it into this playlist

  • @shubhsharma19
    @shubhsharma19 2 года назад

    commenting for algo, great video

  • @paolobonsignore2813
    @paolobonsignore2813 3 года назад +10

    Im kinda confused at this point :/

    • @yanluisnunezlara319
      @yanluisnunezlara319 4 месяца назад +2

      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.

    • @gerardonavarro3400
      @gerardonavarro3400 2 месяца назад +1

      ​@@yanluisnunezlara319fué tremenda explicación mi hermano, muchas gracias!

  • @augischadiegils.5109
    @augischadiegils.5109 3 года назад

    Thanks bro!!

  • @_7.T_
    @_7.T_ 3 года назад

    thank you my Bro ♥♥♥

  • @lamias7712
    @lamias7712 2 года назад

    Merci Bro

  • @joshblank4579
    @joshblank4579 2 года назад

    Bro, how do I make a deep copy of an array?

  • @ivblock4899
    @ivblock4899 Год назад

    what happen if we don't use this while calling copy method and set method

  • @TareqRahaman-u6x
    @TareqRahaman-u6x Год назад

    czx

  • @aboyiphilippa1136
    @aboyiphilippa1136 6 месяцев назад

  • @greeneggsandmushrooms9855
    @greeneggsandmushrooms9855 3 года назад

    now i'm a copy machine

  • @alexyakoveno4682
    @alexyakoveno4682 2 года назад

  • @chakmak
    @chakmak 3 года назад

    where is the new videos?!

  • @drcod3r329
    @drcod3r329 3 года назад

    amazing thats what i can say

  • @shalajko
    @shalajko 3 года назад

    let me just copy this

  • @wallstreetbets7741
    @wallstreetbets7741 2 года назад

    Need rewatch

  • @28santagabo
    @28santagabo 3 месяца назад

  • @SinskariBoi3366three
    @SinskariBoi3366three 7 месяцев назад

    Ai made it easy copy code

  • @kapebreak
    @kapebreak Год назад

    comment

  • @aram8639
    @aram8639 2 года назад

    excellent explanation, love it

  • @mudassirnaz1813
    @mudassirnaz1813 3 года назад

    Thanks

  • @PoorwayTraning
    @PoorwayTraning Год назад

    thanks

  • @baubaudinamo
    @baubaudinamo 3 года назад

    nice

  • @romario92730
    @romario92730 3 года назад

    Thanks, Bro!

  • @skillR-243
    @skillR-243 3 года назад

    Thanks bro

  • @anjapietralla5767
    @anjapietralla5767 9 месяцев назад

  • @maciejkaminski8535
    @maciejkaminski8535 3 года назад

    comment

  • @Simis999
    @Simis999 2 года назад

    thanks