Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation error with generic

Tags:

java

generics

Where is the difference between test1 and test2? Why compilation error in test1?

import java.util.ArrayList;
import java.util.Collection;

class MyType {

}

class MyClass<T> {
    private Collection<MyType> myTypes = new ArrayList<MyType>();
    private Collection<T> myTs = new ArrayList<T>();

    public Collection<MyType> getMyTypes() {
        return myTypes;
    }

    public Collection<T> getMyTs() {
        return myTs;
    }
}



public class TestSimple {

    public void test1() {    

        MyClass myClass = new MyClass();

        for (MyType myType : myClass.getMyTypes())  {

        }
    }

    public void test2() {            
        MyClass myClass = new MyClass();

        Collection<MyType> myTypes = myClass.getMyTypes();
        for (MyType myType : myTypes)  {

        }
    }

    public void test3() {
         MyClass<Long> myClass = new MyClass<Long>();

          for (Long myType : myClass.getMyTs())  {

          }        
     }

}
like image 624
Alex Avatar asked Feb 23 '23 23:02

Alex


2 Answers

If you define a generic constraint on a class, and then instantiate the class without providing any generic constraint (that is, you leave off the <> completely), then you've just stepped into the realm of Raw Types, where nothing is the same anymore.

According to the Java Language Spec:

The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

According to Angelika Langer's excellent Java Generics FAQ,

Methods or constructors of a raw type have the signature that they would have after type erasure. A method or constructor call to a raw type generates an unchecked warning if the erasure changes the argument types.

So by constructing MyClass as a raw type (that is, as MyClass and not MyClass<?>), you have opted out of generics entirely, and the return type of getMyTypes() is now the raw type Collection, and not Collection<MyType>. As a result, you can't use the enhanced for syntax with type MyType, you'd have to use Object instead.

Of course, the better solution is just to use MyClass<?> (rather than just MyClass) when you mean a MyClass of an unknown parameterized type.

like image 189
Daniel Pryden Avatar answered Feb 25 '23 15:02

Daniel Pryden


I've isolated your problem to a smaller example, which I show below

import java.util.*;

public class TestSimple {

    static class MyClass<T> {
        private Collection<String> myTypes = new ArrayList<String>();

        public Collection<String> getMyTypes() {
            return myTypes;
        }
    }

    public void test1() {    
        MyClass myClass = new MyClass();
        for (String myType : myClass.getMyTypes())  {
        }
    }
}

My suspicion is that all type information is stripped off unless you specifically tell it otherwise. So my suggestion is to change your declaration:

before: MyClass myClass = new MyClass();
after:  MyClass<?> myClass = new MyClass();
like image 40
corsiKa Avatar answered Feb 25 '23 15:02

corsiKa