Home » Java programming language

Java Class class getTypeParameters() method with example

Class class getTypeParameters() method: Here, we are going to learn about the getTypeParameters() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 23, 2019

Class class getTypeParameters() method

  • getTypeParameters() method is available in java.lang package.
  • getTypeParameters() method is used to return an array of TypeVariable that denotes the variable type declared by the generic representation of this GenericDeclaration.
  • getTypeParameters() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getTypeParameters() method may throw an exception at the time of returning parameter types.
    GenericSignatureFormatError: This exception may raise when the generic signature of this generic declaration does not match to the format given in the JVM Specification.

Syntax:

    public TypeVariable[] getTypeParameters();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is TypeVariable[], it returns the following value based on the given cases,

  • It returns an array of TypeVariable that denotes the variable type defined by the generic declaration.
  • It returns 0, when the underlying generic declaration defines no variable type.

Example:

// Java program to demonstrate the example 
// of TypeVariable[]  getTypeParameters () method of Class 

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

public class GetTypeParametersOfClass {
 public static void main(String[] args) throws Exception {
  // It returns the array of TypeVariable represented by the 
  //class ArrayList
  TypeVariable[] type_var = ArrayList.class.getTypeParameters();

  for (int i = 0; i < type_var.length; ++i) {
   System.out.print("TypeVariable of ArrayList: " + " ");
   System.out.println(type_var[i].getName());
  }
 }
}

Output

TypeVariable of ArrayList:  E



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.