Home > Back-end >  Javas setter method don't change email when needed
Javas setter method don't change email when needed

Time:07-23

i created setter in sailor class and i want to change email in main class but why my setter wont change the email when its called?

i have tried to change the setter content and implement things to crew class toString method but i cant get it to work, any ideas?


import java.util.ArrayList;

class Sailor {
   private String name;
   private String email;

   public Sailor(String name, String email) {
      this.email = email;
      this.name = name;
   }

   public String getName() {
      return this.name;
   }
   
   public String getEmail() {
      return this.email;
   }

   public void setEmail(String email) {
      this.email = email;
   }
}
    
class Crew {
   ArrayList<Sailor> sailorList = new ArrayList<>();

   public Crew() {}

   public void addCrewMember(Sailor sailor) {
      sailorList.add(new Sailor(sailor.getName(), sailor.getEmail()));
   }

   public String toString() {
      String content = "";

      for (int i = 0; i < sailorList.size(); i  ) {
         content  = sailorList.get(i).getName()   " (";
         content  = sailorList.get(i).getEmail()   ") \n";
      }
      return content;
   }
}
    
class ObjectsCrewProgram {

   public static void main(String[] args) {
      Sailor firstSailor = new Sailor("Frank", "[email protected]");
      Sailor secondSailor = new Sailor("Susan", "[email protected]");
      Sailor thirdSailor = new Sailor("John", "[email protected]");
      Sailor fourthSailor = new Sailor("Ann", "[email protected]");       
      Crew firstCrew = new Crew();
      Crew secondCrew = new Crew();
      firstCrew.addCrewMember(firstSailor);
      firstCrew.addCrewMember(secondSailor);
      firstCrew.addCrewMember(fourthSailor);
      secondCrew.addCrewMember(thirdSailor);
      secondCrew.addCrewMember(secondSailor);
      System.out.println("=== First crew ===\n"   firstCrew); 
      System.out.println("=== Second crew ===\n"   secondCrew);
      secondSailor.setEmail("[email protected]");
      System.out.println("===Second crew ===\n"   secondCrew);
   }
}

CodePudding user response:

why my setter wont change the email when its called?

It does. You're just never observing that change because your program never outputs that value.

You're outputting your crew values:

System.out.println("===Second crew ===\n"   secondCrew);

This uses your .toString() method to generate that output. But what does a "crew" object contain? It doesn't contain the "sailor" objects you provided to it. It contains copies of them:

public void addCrewMember(Sailor sailor) {
    sailorList.add(new Sailor(sailor.getName(), sailor.getEmail()));
}

So your order of operations is:

  1. Create a Sailor object.
  2. Create a Crew object.
  3. Add a copy of the Sailor to the Crew.
  4. Modify the original Sailor.
  5. Print the Crew.

Instead of creating a copy, just add the Sailor itself to the Crew:

public void addCrewMember(Sailor sailor) {
    sailorList.add(sailor);
}

Then any changes you make to those Sailor objects will be visible in that Crew.

CodePudding user response:

You are creating new sailors with this call:

public void addCrewMember(Sailor sailor) {
    sailorList.add(new Sailor(sailor.getName(), sailor.getEmail()));
}

So you are copying the data and not adding the original sailor. So changing the original sailor has no effect.

  •  Tags:  
  • java
  • Related