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 line?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With