Constructor getGenericParameterTypes() method in Java with Examples (original) (raw)

Last Updated : 29 Oct, 2019

The getGenericParameterTypes() method of java.lang.reflect.Constructor class is used to return an array of objects that represent the types of the parameters present on this constructor object. The arrangement Order of the returned array from this method is the same as the order of parameter present on this constructor object. If the constructor has no parameters then the method returns an array of length 0.

Syntax:

public Type[] getGenericParameterTypes()

Parameters: This method accepts nothing.

Return: This method returns an array of Types that represent the formal parameter types of the underlying executable, in declaration order.

Exception: This method throws following exceptions:

Below programs illustrate getGenericParameterTypes() method:
Program 1:

import java.io.IOException;

import java.lang.reflect.*;

public class GFG {

`` public static void main(String[] args)

`` {

`` Class classObj = shape. class ;

`` Constructor[] cons

`` = classObj.getConstructors();

`` Type[] parameters

`` = cons[ 0 ].getGenericParameterTypes();

`` System.out.println( "Parameters : " );

`` for ( int i = 0 ; i < parameters.length; i++)

`` System.out.println(parameters[i]);

`` }

`` public class shape {

`` public shape( int a, long b, double c)

`` {

`` }

`` }

}

Output:

Parameters : class GFG int long double

Program 2:

import java.lang.reflect.Constructor;

import java.lang.reflect.Type;

public class GFG {

`` public static void main(String[] args)

`` {

`` Class classObj = String. class ;

`` Constructor[] cons

`` = classObj.getConstructors();

`` Type[] params

`` = cons[ 0 ].getGenericParameterTypes();

`` System.out.println( "No of Parameters: "

`` + params.length);

`` }

}

Output:

No of Parameters: 3

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