Java copy objects 🖨️

Поділитися
Вставка
  • Опубліковано 18 вер 2024

КОМЕНТАРІ • 104

  • @BroCodez
    @BroCodez  4 роки тому +38

    //***********************************************
    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 Рік тому +19

    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

  • @odaakeenya
    @odaakeenya 2 роки тому +9

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

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 2 роки тому +4

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

  • @yahelbraun5471
    @yahelbraun5471 4 роки тому +12

    Great explanation! Keep on the good work!

    • @BroCodez
      @BroCodez  4 роки тому +3

      thanks for watching Yahel!

  • @dianamilenaarchilacordoba4632
    @dianamilenaarchilacordoba4632 9 днів тому

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

  • @juanestebanparraparra8531
    @juanestebanparraparra8531 2 роки тому +1

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

  • @healer35
    @healer35 Рік тому +1

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

  • @gentle9964
    @gentle9964 Рік тому +1

    excellent explanation, love it

  • @vincentchoo7471
    @vincentchoo7471 3 роки тому +4

    Keep up the great work!

    • @BroCodez
      @BroCodez  3 роки тому +1

      thank you for watching Vincent!

  • @mimvt
    @mimvt Рік тому +1

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

  • @soumelee5661
    @soumelee5661 Рік тому

    wow great explanation

  • @logeshr1761
    @logeshr1761 Рік тому

    Superb explanation bro code

  • @WorstDruidEU
    @WorstDruidEU 2 роки тому +1

    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 роки тому

      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.

  • @Ballnose
    @Ballnose 7 місяців тому

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

  • @ShermukhammadKarimov
    @ShermukhammadKarimov 5 місяців тому

    Thanks for the clear explanation.

  • @eugenezuev7349
    @eugenezuev7349 4 місяці тому

    brilliant

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

  • @theultimateamazing3722
    @theultimateamazing3722 Рік тому

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

  • @elionayzuridasilveira4140
    @elionayzuridasilveira4140 4 місяці тому

    Thank you for this tutorial, Bro!

  • @OmerFarukAY-ij2xg
    @OmerFarukAY-ij2xg 3 місяці тому

    I love you bro

  • @nguyennhan3966
    @nguyennhan3966 Рік тому

    you are the best

  • @geneolo
    @geneolo Рік тому

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

  • @kirillutsenko9306
    @kirillutsenko9306 2 роки тому

    thx for your explanation

  • @pasraju
    @pasraju 2 роки тому

    This is a good explanation!

  • @NikoFool
    @NikoFool 2 роки тому

    Bro code you are a fucking legend!

  • @vaika8817
    @vaika8817 Рік тому

    thanks bro

  • @unalysuf
    @unalysuf Рік тому

    Thank you so much for your effort

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

  • @cristianoseixas2417
    @cristianoseixas2417 Рік тому

    Thanks, excelent

  • @yevgenomelchenko732
    @yevgenomelchenko732 Рік тому

    Thanks, very helpful!

  • @TheEvertonDias
    @TheEvertonDias Рік тому

    Thanks, Bro!

  • @ibtesamatif4418
    @ibtesamatif4418 3 роки тому

    what planet are you from .. ? 100% informative

  • @danny.3036
    @danny.3036 3 роки тому

    Thanks, Bro! ☕ You're awesome!

  • @alexcaesar5377
    @alexcaesar5377 4 роки тому

    Great Job

    • @BroCodez
      @BroCodez  3 роки тому +1

      thanks for watching Metin!

  • @shubhsharma19
    @shubhsharma19 2 роки тому

    commenting for algo, great video

  • @bakaiismaiilov4732
    @bakaiismaiilov4732 3 роки тому

    Love it 👍

  • @danielmilewski7659
    @danielmilewski7659 2 роки тому

    Thanks bro! Awesome channel!

  • @francosalgado4556
    @francosalgado4556 2 роки тому +1

    ¡Muchas gracias!

  • @kaushalprasadyadav9242
    @kaushalprasadyadav9242 2 роки тому

    Please make videos for mobile app development....

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

  • @hannakhamutouskaya2299
    @hannakhamutouskaya2299 3 роки тому

    You are great!!

  • @paolobonsignore2813
    @paolobonsignore2813 3 роки тому +7

    Im kinda confused at this point :/

    • @yanluisnunezlara319
      @yanluisnunezlara319 3 місяці тому

      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 Місяць тому

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

  • @曾毓哲-b1t
    @曾毓哲-b1t Рік тому

    Thank you very much!

  • @stefanescu1000
    @stefanescu1000 9 місяців тому

    It s pretty easy and logic

  • @kemann3815
    @kemann3815 2 роки тому

    Awesome

  • @MrCoolTester
    @MrCoolTester 5 місяців тому +1

    Commenting here just because Bro asked me to do so!

  • @huuloc8719
    @huuloc8719 2 роки тому

    Nice.

  • @tomjohansson3520
    @tomjohansson3520 2 роки тому

    Thanks, very helpfull!

  • @mahdib9361
    @mahdib9361 3 роки тому +1

    nice

  • @alexcaesar5377
    @alexcaesar5377 3 роки тому +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  3 роки тому

      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 3 роки тому

      @@BroCodez could you give me an example?

  • @annexsoftworks
    @annexsoftworks 3 роки тому

    Thanks bro!

  • @mohammadreza1414
    @mohammadreza1414 2 місяці тому

    This is considered a shallow copy right?

  • @SumbaSlice
    @SumbaSlice 3 роки тому

    amazing, thank you!

  • @_7.T_
    @_7.T_ 3 роки тому

    thank you my Bro ♥♥♥

  • @minhtetpaing1695
    @minhtetpaing1695 3 роки тому

    Thank you so much sir.

  • @bakaiismaiilov4732
    @bakaiismaiilov4732 3 роки тому

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

    • @BroCodez
      @BroCodez  3 роки тому +3

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

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 роки тому

    Thanks

  • @kurosshyu2621
    @kurosshyu2621 7 місяців тому

    thanks

  • @augischadiegils.5109
    @augischadiegils.5109 3 роки тому

    Thanks bro!!

  • @Muhammadfaisal-kd9kx
    @Muhammadfaisal-kd9kx 10 місяців тому

    thankyou

  • @ivblock4899
    @ivblock4899 Рік тому

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

  • @suryaa5564
    @suryaa5564 3 роки тому

    Thank You So Much

  • @stephanieokechi1138
    @stephanieokechi1138 3 місяці тому

    🎉

  • @joshblank4579
    @joshblank4579 2 роки тому

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

  • @lamias7712
    @lamias7712 2 роки тому

    Merci Bro

  • @aboyiphilippa1136
    @aboyiphilippa1136 5 місяців тому

  • @greeneggsandmushrooms9855
    @greeneggsandmushrooms9855 3 роки тому

    now i'm a copy machine

  • @drcod3r329
    @drcod3r329 3 роки тому

    amazing thats what i can say

  • @SinskariBoi3366three
    @SinskariBoi3366three 6 місяців тому

    Ai made it easy copy code

  • @alexyakoveno4682
    @alexyakoveno4682 2 роки тому

  • @wallstreetbets7741
    @wallstreetbets7741 2 роки тому

    Need rewatch

  • @shalajko
    @shalajko 3 роки тому

    let me just copy this

  • @chakmak
    @chakmak 3 роки тому

    where is the new videos?!

  • @28santagabo
    @28santagabo 2 місяці тому

  • @user-yq5hj6lg3e
    @user-yq5hj6lg3e Рік тому

    czx

  • @kapebreak
    @kapebreak Рік тому

    comment

  • @aram8639
    @aram8639 2 роки тому

    excellent explanation, love it

  • @romario92730
    @romario92730 3 роки тому

    Thanks, Bro!

  • @skillR-243
    @skillR-243 3 роки тому

    Thanks bro

  • @PoorwayTraning
    @PoorwayTraning Рік тому

    thanks

  • @baubaudinamo
    @baubaudinamo 3 роки тому

    nice

  • @anjapietralla5767
    @anjapietralla5767 8 місяців тому

  • @mudassirnaz1813
    @mudassirnaz1813 3 роки тому

    Thanks

  • @maciejkaminski8535
    @maciejkaminski8535 3 роки тому

    comment

  • @Simis999
    @Simis999 2 роки тому

    thanks