We help IT Professionals succeed at work.

setter method of bean

gudii9 asked

When we can initialize/assign the field values of a class within constructor right like below

sample constructor of Persons object
public Persons(String last, String first, int i)
lastNm = last;
firstNm = first;
age = i;

If this is the case why we need setter methods. I can understand we need to getter to retrieve back those values. please advise
Watch Question

Setter methods are used after the object is created.
You can use a setter to set values initially after you create the object or to change the values.
Graduate Student Research Assistant
Suppose you want to change the age of the person object after it you create it, how are you going to do that? In that case, the setter method helps to change the age of the person.