Field toGenericString() method in Java with Examples
Last Updated :
30 May, 2022
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--
Similar Reads
Field toString() method in Java with Examples The toString() method of java.lang.reflect.Field is used to get a string describing this Field. The format of the returned string is the access modifiers for the field, if any, followed by the field type, followed by a space, followed by the fully-qualified name of the class declaring the field, fol
3 min read
Field toString() method in Java with Examples The toString() method of java.lang.reflect.Field is used to get a string describing this Field. The format of the returned string is the access modifiers for the field, if any, followed by the field type, followed by a space, followed by the fully-qualified name of the class declaring the field, fol
3 min read
Class toGenericString() method in Java with Examples The toGenericString() method of java.lang.Class class is used to convert the instance of this Class to a string representation along with the information about the modifiers and type parameters. This method returns the formed string representation. Syntax: public String toGenericString() Parameter:
2 min read
Class toGenericString() method in Java with Examples The toGenericString() method of java.lang.Class class is used to convert the instance of this Class to a string representation along with the information about the modifiers and type parameters. This method returns the formed string representation. Syntax: public String toGenericString() Parameter:
2 min read
FieldPosition toString() method in Java with Example The toString() method of java.text.FieldPosition class is used to represent the field position object in the form of string. Syntax: public String toString() Parameter: This method does not accepts any argument as parameter. Return Value: This method returns the string representation of this FieldPo
2 min read
Field set() method in Java with Examples The set() method of java.lang.reflect.Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter. The new value is automatically unwrapped if the underlying field has a primitive type. If the field is s
4 min read