How to instantiante object & call setter on same line?
If I have an Employee
class with a default constructor:
private String firstName;
public Employee(){}
and a setter:
public void setFirstName(String firstName){
this.firstName = firstName;
}
Why does this attempt fail to instantiate and call the setter in the same li开发者_StackOverflowne?
Employee employee = new Employee().setFirstName("John");
You can also use this syntax:
Employee employee = new Employee() {{
setFirstName("John");
}};
Though keep in mind that it's going to create an anonymous inner class and probably isn't what you want.
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.
(employee = new Employee()).setFirstName("John");
performs instantiation and calling the setter, as you requested in the headline, but does not declare the variable as suggested in your code example.
(Employee employee = new Employee()).setFirstName("John");
will probably not work, I assume. But you can try.
Of course, you can always stuff multiple statements in one line.
Employee employee; (employee = new Employee()).setFirstName("John");
or
Employee employee = new Employee(); employee.setFirstName("John");
If I were you, I would settle for a parameterized constructor, though.
The method serFirstName
is of return type void
(nothing). Try:
public Employee setFirstName(String fname) {
this.firstName = fname;
return this;
}
It should be like this:
Employee employee = new Employee();
employee.setFirstName("John");
Although this is a bit overkill, you could try using the builder pattern
public class Employee{
private String firstName;
public static class Builder{
private String firstName;
public Builder firstName(String firstName){
this.firstName = firstName;
return this;
}
public Employee build(){
return new Employee(this);
}
}
private Employee(Builder builder){
firstName = builder.firstName;
}
}
Then you can do the following
Employee e = new Employee.Builder().firstName("John").build();
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.
In order for your code to work, you would have to return the Employee
(meaning "this") in the setter method setFirstName
.
If you don't own the Employee class (I know this is just a simple example - but for the sake of argument) and cannot modify it, one way to solve that is using functional programming. You could declare yourself a function like this:
static final Function<String, Employee> EMPLOYEE = firstName -> {
Employee employee = new Employee();
employee.setFirstName(firstName);
return employee;
};
And then you can create your employee in one line like this:
Employee jake = EMPLOYEE.apply("Jake");
Maybe not exactly what you want, but still useful.
精彩评论