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:
// 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()); } } } |
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 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 ; } |
toGenericString is public static short Numbers.value
References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#toGenericString–