I have a generic base class:
public class BaseLabel<T> extends JXLabel {
private static final long serialVersionUID = 1L;
public BaseLabel() {
System.out.println(T.class.getClass().getName()); //error
}
}
and Child class:
public class ChildLabel extends BaseLabel<ChildLabel> {
private static final long serialVersionUID = 2L;
public ChildLabel() {
}
}
I am getting compilation error.
Is there any way to get the actual class name from the constructor of BaseClass. Here, by actual class I am referring to that class, which I am going to instantiate.
For example I am instantiating ChildClass, ChildClass clz = new ChildClass();
then that println() will print package.ChildClass.
Thanks in advance.
The compilation error is:
cannot select from a type variable System.out.println(T.class.getClass().getSimpleName());
If I call this.getClass.getSimpleName() from the abstract BaseClass‘s constructor. It is printing the ChildClass.
Why?
Is it due to, as I am instantiating ChildClass so this is pointing to the ChildClass‘s object.
Ugly? yes
child class