This simply means that the type of T
must be a class that extends Builder
. See this example:
public class ABuilder extends Builder<ABuilder> {
}
This construct is commonly seen when using the builder pattern, because it allows a super class to return an instance of the subclass so that subclass methods may still be used. From the example you've shown:
public T calories(int val) {
calories = val;
return (T) this;
}
Will allow that this code snippet works:
NutritionFacts nf = new ABuilder()
.specificMethodForABuilder() // returns ABuilder
.calories(3) // returns also ABuilder
.anotherSpecificMethodForABuilder() // returns again ABuilder
.build();
The same would not work if the calories()
method would've been declared like this (and the builder without a generic T
):
public Builder calories(int val) {
calories = val;
return this;
}
You would get a compilation error if you tried this:
NutritionFacts nf = new ABuilder()
.specificMethodForABuilder() // returns ABuilder
.calories(3) // returns only Builder
.anotherSpecificMethodForABuilder() // unknown method
.build();