Java generics + Builder pattern

You were close:

Foo.Builder.<Bar> start().setCount(1).setKey(bar).build();

Cheers! 🙂

P.S. If the compiler can’t infer the type parameter of the method on its own, you can force it by calling obj.<Type> method(...) .

P.P.S you might want to use:

public Foo<K2> build() {
    return new Foo<K2>(this);
}

Avoid using raw types.

Leave a Comment

tech