I was going throught this link
Java Generic Class - Determine Type
I tried with this program.
package my;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class GenericTypeIdentification {
public static void main( String args[] ) {
Node<Integer> obj = new GenericTypeIdentification().new SubNode<Integer>( 1 );
ParameterizedType parameterizedType = ( ParameterizedType ) obj.getClass().getGenericSuperclass();
Type clazz = parameterizedType.getActualTypeArguments()[0];
if ( clazz == Integer.class ) {
System.out.println( 1 );
}
else {
System.out.println( 2 );
}
}
class Node<T> {
private final T value;
public Node( T val ) {
this.value = val;
}
public T evaluate() {
return value;
};
}
class SubNode<T> extends Node<T> {
private final T value;
public SubNode( T val ) {
super( val );
value = val;
}
@Override
public T evaluate() {
return value;
};
}
}
My understanding was that it should printh output as 1
but it prints 2
. Please help me in understanding this. Thanks.