Field getGenericType() method in Java with Examples

The getGenericType() method of java.lang.reflect.Field used to return a Type object representing the declared type of this Field object. The returned type object can be one of the implementations of Type’s subinterfaces: GenericArrayType, ParameterizedType, WildcardType, TypeVariable, Class. If the Type of Field object is a parameterized type, the returned Type object must accurately reflect the actual type parameters used in the source code and if the type of the underlying field is a type variable or a parameterized type, it is created. Otherwise, it is resolved.

Syntax:

public Type getGenericType()

Parameters: This method accepts accepts nothing.



Return: This method returns a Type object that represents the declared type for the field represented by this Field object.

Exception: This method returns the following Exceptions:

  • GenericSignatureFormatError: if the generic field signature does not conform to the format specified in The Java™ Virtual Machine Specification.
  • TypeNotPresentException: if the generic type signature of the underlying field refers to a non-existent type declaration.
  • MalformedParameterizedTypeException: if the generic signature of the underlying field refers to a parameterized type that cannot be instantiated for any reason.

Below programs illustrate getGenericType() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// getGenericType() method
  
import java.lang.reflect.Field;
import java.lang.reflect.Type;
  
public class GFG {
  
    // initialize field
    private static int number;
  
    public static void main(String[] args)
        throws NoSuchFieldException
    {
        // get Field object
        Field field
            = GFG.class
                  .getDeclaredField("number");
  
        // apply getGenericType() method
        Type type = field.getGenericType();
  
        // print Results
        System.out.println(
            "Type class: "
            + type.getClass());
        System.out.println(
            "Type name: "
            + type.getTypeName());
    }
}

chevron_right


Output:

Type class: class java.lang.Class
Type name: int

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// getGenericType() method
  
import java.lang.reflect.Field;
import java.lang.reflect.Type;
  
public class GFG {
  
    // initialize field
    final static String value = "Geeks";
  
    public static void main(String[] args)
        throws NoSuchFieldException
    {
  
        // get Field object
        Field field
            = GFG.class
                  .getDeclaredField("value");
  
        // apply getGenericType() method
        Type type = field.getGenericType();
  
        // print Results
        System.out.println(
            "Type class: "
            + type.getClass());
        System.out.println(
            "Type name: "
            + type.getTypeName());
    }
}

chevron_right


Output:

Type class: class java.lang.Class
Type name: java.lang.String

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getGenericType–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.