Constructor getTypeParameters() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Constructor.getTypeParameters() method
  
import java.lang.reflect.Constructor;
import java.lang.reflect.TypeVariable;
  
public class GFG {
  
    public static void main(String... args)
        throws NoSuchMethodException
    {
  
        // Create Test class
        Class<Test> cls = Test.class;
  
        // Create Constructor Object
        Constructor[] constructors
            = cls.getConstructors();
  
        // get Annotation array
        TypeVariable[] typeVariables
            = constructors[0].getTypeParameters();
  
        System.out.println("TypeVariables:"
                           + typeVariables);
    }
}
class Test<N extends Number> {
    public Test(N n) {}
}

chevron_right


Output:

TypeVariables:[Ljava.lang.reflect.TypeVariable;@15db9742

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Constructor.getTypeParameters() method
  
import java.lang.reflect.TypeVariable;
  
public class GFG {
  
    public static void main(String... args)
    {
  
        // get Type Parameters
        TypeVariable<Class<GFG_Demo> >[] typeParameters
            = GFG_Demo.class.getTypeParameters();
  
        // print result
        for (TypeVariable<Class<GFG_Demo> >
                 typeParameter : typeParameters) {
            System.out.println(typeParameter);
        }
    }
  
    private static class GFG_Demo<N, E> {
    }
}

chevron_right


Output:

N
E

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getTypeParameters()

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.