Matthew
Matthew

Reputation: 867

Generic type T hiding type

There are numerous questions on this issue. However, my particular case does not deal with interface declarations but rather method return types. The java documentation on generics used with methods says this is legal: public <T> boolean isType(ArrayList<T> a) { https://docs.oracle.com/javase/tutorial/extra/generics/methods.html

However, i get the error The type parameter T is hiding the type T

The code itself functions fine, but my question is, what is actually happening here? Should I be paying any mind to this/does this pose problems down the rode...?

Here is the code in full:

import java.util.ArrayList;

public class Generics<T, K> {

   private T obj1;
   private K obj2;

   public Generics(T obj1, K obj2) {
       this.obj1 = obj1;
       this.obj2 = obj2;
   }

   public T getObj1() {
       return obj1; 
   }

   public <T> boolean isType(ArrayList<T> a) {//Error on this line
       return a.equals(obj1); //I realize this does not get the type.
   }

}

I appreciate the help

Upvotes: 1

Views: 65

Answers (1)

Ivan
Ivan

Reputation: 8758

You are already using T in declaration of your generic class. You need to use another alias for the method like

public <U> boolean isType(ArrayList<U> a) {
    return a.equals(obj1); 
}

Upvotes: 4

Related Questions