Field toGenericString() method in Java with Examples

The toGenericString() method of java.lang.reflect.Field is used to return a string which represents this Field, including its generic type. The format of the string is the access modifiers for the field, if any, followed by the generic field type, followed by a space, followed by the fully-qualified name of the class declaring the field, followed by a period, followed by the name of the field.

The modifiers are placed in canonical order as specified by “The Java Language Specification”. This is public, protected or private first, and then other modifiers in the following order: static, final, transient, volatile.

Syntax:



public String toGenericString()

Parameters: This method accepts accepts nothing.

Return: This method returns a string describing this Field, including its generic type.

Below programs illustrate toGenericString() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// toGenericString() method
  
import java.lang.reflect.Field;
import java.time.Month;
  
public class GFG {
  
    public static void main(String[] args)
        throws Exception
    {
  
        // Get all field objects of the Month class
        Field[] fields
            = Month.class.getFields();
  
        for (int i = 0; i < fields.length; i++) {
  
            // print name of Fields
            System.out.println(
                "toGenericString of Field:\n"
                + fields[i].toGenericString());
        }
    }
}

chevron_right


Output:

toGenericString of Field:
public static final java.time.Month java.time.Month.JANUARY
toGenericString of Field:
public static final java.time.Month java.time.Month.FEBRUARY
toGenericString of Field:
public static final java.time.Month java.time.Month.MARCH
toGenericString of Field:
public static final java.time.Month java.time.Month.APRIL
toGenericString of Field:
public static final java.time.Month java.time.Month.MAY
toGenericString of Field:
public static final java.time.Month java.time.Month.JUNE
toGenericString of Field:
public static final java.time.Month java.time.Month.JULY
toGenericString of Field:
public static final java.time.Month java.time.Month.AUGUST
toGenericString of Field:
public static final java.time.Month java.time.Month.SEPTEMBER
toGenericString of Field:
public static final java.time.Month java.time.Month.OCTOBER
toGenericString of Field:
public static final java.time.Month java.time.Month.NOVEMBER
toGenericString of Field:
public static final java.time.Month java.time.Month.DECEMBER

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// toGenericString() method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws Exception
    {
  
        // create Numbers object
        Numbers no = new Numbers();
  
        // Get the value field object
        Field field
            = Numbers.class.getField("value");
  
        // print value of isActive
        System.out.println(
            "toGenericString is\n"
            + field.toGenericString());
    }
}
  
// sample Numbers class
class Numbers {
  
    // static short value
    public static short value = 13685;
}

chevron_right


Output:

toGenericString is 
public static short Numbers.value

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



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.