import java.util.*;
import java.lang.*;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) throws Exception {
        TypeReference<X> ref = new TypeReference<X>() {};
        System.out.println(ref.getType());
        System.out.println(ref.getType().getClass());

        Class<?> clazz = (Class<?>) ref.getType();
        Object obj = clazz.getDeclaredConstructor().newInstance();
        System.out.println("'" + obj + "'");
        Object obj2 = clazz.getDeclaredConstructor(String.class).newInstance("X2");
        System.out.println("'" + obj2 + "'");
    }
}

class TypeReference<T> {
    Type type;
    TypeReference() {
        type = ((ParameterizedType) getClass()
                   .getGenericSuperclass())
                   .getActualTypeArguments()[0];
    }
    public Type getType() { return type; }
}

class X {
    private String _x;
    
    public X() {
        _x = "X";
    }
    
    public X(String x) {
        _x = x;
    }

    @Override
    public String toString() {
        return "hello, " + _x;
    } 
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: