OK this is my class, it encapsulates an object, and delegates equals and to String to this object, why I can´t use instance of???
public class Leaf<L>
{
private L object;
/**
* @return the object
*/
public L getObject() {
return object;
}
/**
* @param object the object to set
*/
public void setObject(L object) {
this.object = object;
}
public boolean equals(Object other)
{
if(other instanceof Leaf<L>) //--->ERROR ON THIS LINE
{
Leaf<L> o = (Leaf<L>) other;
return this.getObject().equals(o.getObject());
}
return false;
}
public String toString()
{
return object.toString();
}
}
how can I get this to work?? Thanks!
Due to type erasure you can only use instanceof
with reifiable types. (An intuitive explanation is that instanceof
is something that is evaluated at runtime, but the type-parameters are removed ("erased") during compilation.)
Here is a good entry in a Generics FAQ:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With