Java copy objects 🖨️

Поділитися
Вставка

КОМЕНТАРІ • 106

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

    //***********************************************
    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 роки тому +6

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

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

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

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

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

  • @healer35
    @healer35 2 роки тому +3

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

  • @dianamilenaarchilacordoba4632
    @dianamilenaarchilacordoba4632 4 місяці тому +2

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

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

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

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

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

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

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

    Superb explanation bro code

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

    Keep up the great work!

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

      thank you for watching Vincent!

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

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

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

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

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

    excellent explanation, love it

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

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

    Great video!!!!!!

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

    Thanks for the clear explanation.

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

    Thank you for this tutorial, Bro!

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

    you are the best

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

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

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

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

    This is a good explanation!

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

    Thanks, excelent

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

    Thanks bro! Awesome channel!

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

    thx for your explanation

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

    Thank you so much for your effort

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

    Thanks, Bro! ☕ You're awesome!

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

    Thanks, very helpful!

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

    Love it 👍

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

    Awesome

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

    I love you bro

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

    Great Job

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

      thanks for watching Metin!

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

    Thank you very much!

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

    🎉

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

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

    nice

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

    Nice.

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

    ¡Muchas gracias!

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

    what planet are you from .. ? 100% informative

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

    brilliant

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

    Bro code you are a fucking legend!

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

    thank you my Bro ♥♥♥

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

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

    Thanks, very helpfull!

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

    You are great!!

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

    Please make videos for mobile app development....

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

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

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

      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

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

    commenting for algo, great video

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

    amazing, thank you!

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

    This is considered a shallow copy right?

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

    Thanks bro!

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

    Commenting here just because Bro asked me to do so!

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

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

    Thanks, Bro!

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

    It s pretty easy and logic

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

    Thanks bro

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

    Thank you so much sir.

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

    Thanks

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

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

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

    Thanks bro!!

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

    Thank You So Much

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

    Merci Bro

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

    Im kinda confused at this point :/

    • @yanluisnunezlara319
      @yanluisnunezlara319 7 місяців тому +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 6 місяців тому +1

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

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

    thanks

  • @Muhammadfaisal-kd9kx
    @Muhammadfaisal-kd9kx Рік тому

    thankyou

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

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

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

    amazing thats what i can say

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

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

    now i'm a copy machine

  • @ap1136-c4m
    @ap1136-c4m 10 місяців тому

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

  • @TareqRahaman-u6x
    @TareqRahaman-u6x Рік тому

    czx

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

    where is the new videos?!

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

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

    let me just copy this

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

    Need rewatch

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

    Ai made it easy copy code

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

    comment

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

    excellent explanation, love it

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

    Thanks, Bro!

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

    thanks bro

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

    nice

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

    Thanks

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

    thanks

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

    comment

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

    thanks