-2

I don't know why we need setters for this program when the constructor does the same job. I already referenced name to aName in the constructor. I dont know why you have to do the same thing in the setter. Same with salary = aSalary

public class Employer
{
    private String name;
    private double salary;

    public Employer(String aName, double aSalary)
    {
        name = aName;//I already referenced name to aName here
        salary = aSalary;
    }

    public void setName(String aName)
    {
        name = aName;//I don't know why I have to do it again here
    }

    public String getName()
    {
        return name;
    }

    public void setSalary(double aSalary)
    {
        salary = aSalary;//I dont know why we do it here too
    }

    public double getSalary()
    {
        return salary;
    }

Please Help!!!

  • At a broad level, it is essentially because you have by design chosen this class objects to be mutable. If you don't want to change the object at a later stage, you can remove the setters. – achin Dec 11 '15 at 03:22
  • 4
    Possible duplicate of [Java Setter and Constructor confusion](http://stackoverflow.com/questions/19163598/java-setter-and-constructor-confusion) – nubteens Dec 11 '15 at 03:23

1 Answers1

2

Because nobody ever changes their name or salary?

Employer e = new Employer("Bruce Jenner", 1.0);
// time passes
e.setName("Caitlyn Jenner");
John Hascall
  • 9,176
  • 6
  • 48
  • 72