Generic Types in static methods vs instance methods

Viewed 83

I have a class named A (without generics), then if you look at inside the main method, everything is fine.

class A {

    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        staticMethod(stringList); //DOES NOT COMPILE 
        new A().instanceMethod(stringList); //DOES NOT COMPILE
    }

    static void staticMethod(List<Integer> aList) { }

    void instanceMethod(List<Integer> aList) { }
}

But when I have the class A with a generic,

class A<T> {

    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        staticMethod(stringList); //DOES NOT COMPILE 
        new A().instanceMethod(stringList); //**DOES COMPILE!!**
    }

    static void staticMethod(List<Integer> aList) { }

    void instanceMethod(List<Integer> aList) { }
}

I don't understand, how the raw usage of the class' instance make it compilable ?

Thank you.

0 Answers
Related