Search code examples
javagenericstype-parameterreference-typeshadowing

do generic type param shadow reference types? (java)


Let's say we have the following scenario:

public class SomeClass {

}

And now we have a generic class with type parameter SomeClass instead of T.

public class GenericClass <SomeClass> {
    List<SomeClass> list;

    public GenericClass() {
         list = new ArrayList<>();
    }

    public void add(SomeClass obj) {
         list.add(obj);
    }
}

Will the type parameter SomeClass shadow the reference datatype SomeClass in the whole GenericClass?


Solution

  • Yes. That's a name shadow in this context. You can still disambiguate with the fully qualified class name (as long you as you use a package, as you should). Also, this is a good argument for using conventional short type names for your generic types.