Skip to content
Advertisement

Passing generic types inner class

Here is following example:

JavaScript

Is there a way just to pass the generic <String> one time? Actually the inner class should already know its type.

JavaScript

Advertisement

Answer

That is a limitation of the Java compiler. Although it looks obvious in this case that the type parameter must be String, inferring that is not as easy as it seems to be: The compiler has to propagate the type information backwards from build() via test("my object") to the builder() method.

New compiler versions may support such inference, but at least javac 11 does not.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement