2
public class Person
{
    private String name;
    private String streetAddress;
    private String cityStateZip;

    public Person()
    {
        name = "John Doe";
        streetAddress = "55 Main Street";
        cityStateZip = "Anywhere, MA 00001";
    }

    public Person(String n, String s, String c)
    {
        name = n;
        streetAddress = s;
        cityStateZip = c;
    }
    public String getName()
    {
        return this.name;
    }
    public void setName()
    {
    this.name = name;
    }
    public String getStreetAddress()
    {
        return this.streetAddress;
    }
    public void setStreetAddress()
    {
        this.streetAddress = streetAddress;
    }
    public String getCityStateZip()
    {
        return this.cityStateZip;
    }
    public void setCityStateZip()
    {
        this.cityStateZip = cityStateZip;
    }
    public String toString()
    {
        String str =    name + "\n" + streetAddress + "\n" + cityStateZip + "\n";
        return str;
    }

}

In the code above I have written a class that will create a person object and will be used to create other classes that will implement this person class. While I was writing this superclass I thought, what is the difference between setter methods and constructors? Are setter methods simply helpful because you can easily just do .set(methodName) or? Would someone be willing to explain the difference between my constructor and my setter method?

  • 1
    [Well-Explained](https://stackoverflow.com/questions/43174735/difference-between-constructor-and-getter-and-setter) – thar45 Mar 17 '19 at 04:15

3 Answers3

2

Constructors construct new object, where as setters are created to update that object.

So lets say your name is Ding Dong and you live at, and you create a person object with your name and following address.

12345 15th Street
Area 51, Nevada 12345

and you move couple of blocks down the street to:

1234 15th Street
Area 51, Nevada 12345

Well you do not need to create new Person object because you moved, you just need to update address. That is where you will use setter method.

Conclusion: Setters are there to update your record and constructors are there to create new Person object.

I hope this helps!!!!

1

Setter sets the values of Objects attributes that was earlier created. Constructor in your case creates the object with a bunch of predefined values.

Class Pizza
Attributes: boolean Extra Cheese , boolean Extra mayonnaise

Ex Constructor Call:

Pizza Margherita = new Pizza(true, false) //sets value of attributes, i.e you want extra cheese or mayonnaise?

Ex setter Call:

Pizza Marinara= new Pizza();
public void setCheese (boolean cheese ) {      //sets the values explictly 
        this.cheese  = cheese ;
    }
public void setMayonnaise (boolean mayonnaise) {
        this.mayonnaise= mayonnaise;             //sets the value explictly
    }

We should use constructor approach when we need need to create object with attributes values than explicitly calling setters for each values and setting them.

Though both have different advantages and are well suited in Java ecosystem. If you want to have an immutable class use constructor otherwise use setters.

If you want to have an immutable class use constructor otherwise use setters.

With Setters(), or mutators, provide the caller with an opportunity to update the value of a particular instance variable.


More:

Constructor need not have Access specifier , return type , while setters need to have access specifier.

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
1

Setters

  1. Setter methods can have the business logic & validations for incoming data. They serve best especially when it comes to objects whose states are transient or alter a lot
  2. Setter methods are more helpful when you use them along-with getter methods because that way it strengthens the 'Encapsulation' property of OOPs in Java. Your instance variables can be completely private. You can even keep your object also hidden from outside world. You can check out private constructors & factory methods for your reference
  3. Return type needs to be mentioned explicitly

Constructor

  1. It is used only at the time of object creation so whatever logic & validations you code will need to be again separately implemented in other method for updating the data of the object
  2. They have an implicit return type so we don't need to write anything. If we write any return type, then that method is not treated as constructor
Sharad Nanda
  • 406
  • 1
  • 15
  • 27