The getTypeParameters() method of a Constructor class is used to get an array of TypeVariable objects declared by this Constructor Object, in declaration order. Elements of array represent the type variables objects declared by Method. An array of length 0 is returned by this getTypeParameters(), if the Method Object generic declaration contains no type variables.
Syntax:
public TypeVariable<Constructor<T>>[] getTypeParameters()
Parameters: This method does not accept any parameters.
Return value: This method returns an array of TypeVariable objects that represent the type variables declared by this generic declaration.
Exception: This method throws GenericSignatureFormatError if the generic signature of this generic declaration does not conform to the format specified in The Javaâ„¢ Virtual Machine Specification.
Below programs illustrate the getTypeParameters() method:
Program 1:
Java
import java.lang.reflect.Constructor;
import java.lang.reflect.TypeVariable;
public class GFG {
public static void main(String... args)
throws NoSuchMethodException
{
Class<Test> cls = Test. class ;
Constructor[] constructors
= cls.getConstructors();
TypeVariable[] typeVariables
= constructors[ 0 ].getTypeParameters();
System.out.println("TypeVariables:"
+ typeVariables);
}
}
class Test<N extends Number> {
public Test(N n) {}
}
|
Output:
TypeVariables:[Ljava.lang.reflect.TypeVariable;@15db9742
Program 2:
Java
import java.lang.reflect.TypeVariable;
public class GFG {
public static void main(String... args)
{
TypeVariable<Class<GFG_Demo> >[] typeParameters
= GFG_Demo. class .getTypeParameters();
for (TypeVariable<Class<GFG_Demo> >
typeParameter : typeParameters) {
System.out.println(typeParameter);
}
}
private static class GFG_Demo<N, E> {
}
}
|
Output:
N
E
References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getTypeParameters()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Apr, 2023
Like Article
Save Article