Open In App

Field toGenericString() method in Java with Examples

Last Updated : 30 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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  nothing. Return: This method returns a string describing this Field, including its generic type. Below programs illustrate toGenericString() method: 

Program 1: 

Java




// 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());
        }
    }
}


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: 

Java




// 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;
}


Output:

toGenericString is 
public static short Numbers.value

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads