Calling static generic methods

JavaGenericsStaticType Inference

Java Problem Overview


I have come across a curious situation involving static generic methods. This is the code:

class Foo<E>
{
    public static <E> Foo<E> createFoo()
    {
        // ...
    }
}

class Bar<E>
{
    private Foo<E> member;

    public Bar()
    {
        member = Foo.createFoo();
    }
}

How come I don't have to specify any type arguments in the expression Foo.createFoo()? Is this some kind of type inference? If I want to be explicit about it, how can I specify the type argument?

Java Solutions


Solution 1 - Java

Yes, this is type inference based on the target of the assignment, as per JLS section 15.12.2.8. To be explicit, you'd call something like:

Foo.<String>createFoo();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfredoverflowView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow