Java copy objects 🖨️

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

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

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

    //***********************************************
    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

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

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

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

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

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

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

  • @toxicwaste6132
    @toxicwaste6132 9 дней назад

    Really helpful for someone who is learning Java in university thank you my man!

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

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

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

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

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

    Great explanation! Keep on the good work!

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

      thanks for watching Yahel!

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

    wow great explanation

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

    Thanks for the clear explanation.

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

    Superb explanation bro code

  • @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.

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

    excellent explanation, love it

  • @nguyenminhquang7052
    @nguyenminhquang7052 День назад

    Great video!!!!!!

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

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

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

    thx for your explanation

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

    Thanks, excelent

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

    nice

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

    Awesome

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

    you are the best

  • @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

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

    Keep up the great work!

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

      thank you for watching Vincent!

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

    Great Job

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

      thanks for watching Metin!

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

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

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

    Nice.

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

    Thank you so much for your effort

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

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

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

    brilliant

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

    Thank you for this tutorial, Bro!

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

    Love it 👍

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

    Commenting here just because Bro asked me to do so!

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

    This is a good explanation!

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

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

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

    Please make videos for mobile app development....

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

    Thank you very much!

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

    Thanks, very helpful!

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

    🎉

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

    Im kinda confused at this point :/

    • @yanluisnunezlara319
      @yanluisnunezlara319 5 месяцев назад +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 3 месяца назад +1

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

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

    I love you bro

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

    You are great!!

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

    ¡Muchas gracias!

  • @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!

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

    what planet are you from .. ? 100% informative

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

    Thanks bro

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

    This is considered a shallow copy right?

  • @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?

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

    It s pretty easy and logic

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

    Thanks bro! Awesome channel!

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

    Thanks, very helpfull!

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

    Thank you so much sir.

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

    Thanks

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

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

  • @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

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

    Thank You So Much

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

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

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

    Thanks, Bro! ☕ You're awesome!

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

    thanks

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

    Merci Bro

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

    Thanks, Bro!

  • @Muhammadfaisal-kd9kx
    @Muhammadfaisal-kd9kx Год назад

    thankyou

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

    thank you my Bro ♥♥♥

  • @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

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

    Bro code you are a fucking legend!

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

    Thanks bro!

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

    amazing, thank you!

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

    Thanks bro!!

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

    czx

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

    where is the new videos?!

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

    now i'm a copy machine

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

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

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

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

    amazing thats what i can say

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

    let me just copy this

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

    Need rewatch

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

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

    Ai made it easy copy code

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

    comment

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

    excellent explanation, love it

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

    nice

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

    thanks bro

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

    Thanks

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

    Thanks, Bro!

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

    thanks

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

    comment

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

    thanks