开发者

Calling static generic methods

开发者 https://www.devze.com 2023-02-16 12:46 出处:网络
I have come across a curious situation involving static generic methods. This is the co开发者_开发问答de:

I have come across a curious situation involving static generic methods. This is the co开发者_开发问答de:

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?


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消