class Animal{ // class implementation } public class Main { public static void main(String[] args) { Class c = Animal.class; TypeVariable[] typeParams = c.getTypeParameters(); System.out.println(Arrays.toString(typeParams)); } }
class PairIn this example, we define a generic class Pair with two type parameters K and V. Then, we get the Class object for Pair and use the getTypeParameters() method to get an array of TypeVariable objects representing the type parameters of the class. This will print out [K, V]. The package library for the getTypeParameters() method is the java.lang.reflect package.{ private K key; private V value; public Pair(K key, V value) { this.key = key; this.value = value; } // other class methods } public class Main { public static void main(String[] args) { Class c = Pair.class; TypeVariable[] typeParams = c.getTypeParameters(); System.out.println(Arrays.toString(typeParams)); } }