Invoking statically imported method with explicit type parameters

Viewed 6207

This is the follow up of my question here: Weird Java generic.

If I have a code like this:

Casts.<X, T> cast(iterable[index]);

Can I add a static import and do:

<X, T> cast(iterable[index]);

Eclipse doesn't allow this. But after seeing so many bugs with static import in Eclipse, I'm not that certain.

5 Answers

No, you can't : I just confirmed this via some test code.

PS > javac -version
javac 1.6.0_04

Casts.java

public class Casts
{
    public static <From, To> To cast(final From object)
    {
        return (To)object;
    }
}

Test.java

import static Casts.cast;

public class Test
{
    public static void main(String[] args)
    {
        final Integer integer = new Integer(5);

        // This one compiles fine.
        final Number number = Casts.<Integer, Number>cast(integer);

        // This one fails compilation:
        // PS> javac Test.java
        // Test.java:11: illegal start of expression
        //             final Number number = <Integer, Number>cast(integer);
        //                                                    ^
        // Test.java:11: not a statement
        //             final Number number = <Integer, Number>cast(integer);
        //                                                        ^
        final String string = <Integer, String>cast(integer);
    }
}

No

If you want to provide an explicit type parameter when calling a generic static method, you must prefix the method with the class name, even if the method is statically imported.

I'm pretty sure the answer is no--if you want to use a generic method call, you need an object to call it on (foo.<T>doSomething()). If the method is static, you need the class ( Foo.<T>doSomething() ).

This is even true if you're calling the method from elsewhere in the class itself. If you are working in a non-static method (i.e. in an instance method), you would call this.<T>doSomething().

Related