I am using generics in my class. MyObject must use T as an class, e.g. new MyObject(Long.class). In the method equal I want to ensure that the argument given to the method is an instance of T.
Object<T> is not a valid Java Source, I know.
I added it to the code example to show you what I mean, i.e. that value is an instance of T.
But what is the correct way to implement the equal method??
public class MyObject<T extends Class> {
public boolean equal(Object<T> value) {
return true;
}
}
Assuming that you’re not out to override
Object.equals(Object):T extends Classmakes scant sense becauseClassis final anyway, so the only thingTcould be would beClass<U>for someU, and then you might as well letUbe the type parameter. So what you might want it something like:and you then get to write, say,
new MyClass<Long>(Long.class).But are you sure you need to store the
clparameter at runtime at all? Normally you would leave it out completely, so you just writenew MyClass<Long>().