If I have an Employee
class with a default constructor:
private String firstName;
public Employee(){}
and a setter:
Because the you want to set employee
to the value of .setFirstName("John");
which does not return anything because it is a void
So you could either change your setter to:
public Employee setFirstName(String fname) {
this.firstName = fname;
return this;
}
OR Create a second constructor for Employee
public Employee(String fname){this.firstName = fname;}
Which would set firstname
on init.
Because setFirstName
doesn't return anything. If you want to chain methods then setFirstName
would have to return Employee
.
Another approach is to have a constructor that takes firstName
as an argument.