Answer the question
In order to leave comments, you need to log in
What is this use of return this in Java class setters called?
public class User {
private String name;
private String surname;
private Integer age;
public String getName() {
return name;
}
public User setName(String name) {
this.name = name;
return this;
}
public String getSurname() {
return surname;
}
public User setSurname(String surname) {
this.surname = surname;
return this;
}
public Integer getAge() {
return age;
}
public User setAge(Integer age) {
this.age = age;
return this;
}
}
Answer the question
In order to leave comments, you need to log in
method chaining.
A method is useful when you want to call a number of methods on an object. Allows you to get rid of redundant code.
However, such an implementation is almost never used in modern Java. Either builders or factory methods are used (see JEP 269).
Distribution received Function chaining. But that's another answer.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question