I have a problem figuring out how to check which class a generic variable has at runtime. I have the following simplified class.
public class Link<E> {
private final String type;
private E e;
public Link() {
// TODO find out the class of 'E' and set 'type' to name it (i.e if E is a Float I want 'type = "Float"'
}
public Link(E e) {
// TODO find out the class of 'E' and set 'type' to name it (i.e if E is a Float I want 'type = "Float"'
this.e = e;
}
public synchronized E get() {return e;}
public synchronized void set(E e) {
this.e = e;
}
@Override
public synchronized String toString() {
return String.format("< %s > [ %s ]", type, (e == null ? "'null'" : e.toString()));
}
}
With this class I can create an instance of it by the following command
Link<Integer> link = new Link<Integer>();
If I then call the toString method of this instance I want to receive the String "< Integer > [ 'null' ]" but I have no idea how to find out the type of the class in the constructor. Anyone knows how to do this?