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, followed by a period, followed by the name of the field.
For example:
public static final Month java.time.Month.APRIL
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 toString()
Parameters: This method accepts nothing.
Return value: This method returns a string describing this Field.
Below programs illustrate toString() method:
Program 1:
// Java program to illustrate toString() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // Get the array of field object Field[] fields = User.class.getFields(); // loop for (int i = 0; i < fields.length; i++) { // get the string representation of each field String field = fields[i].toString(); System.out.println("Field: " + field); } } } // sample User class class User { // static double values public static double Marks = 34.13; public static float Fees = 3413.99f; public static String name = "Aman"; public static double getMarks() { return Marks; } public static void setMarks(double marks) { Marks = marks; } public static float getFees() { return Fees; } public static void setFees(float fees) { Fees = fees; } public static String getName() { return name; } public static void setName(String name) { User.name = name; } } |
Field: public static double User.Marks Field: public static float User.Fees Field: public static java.lang.String User.name
Program 2:
// Java program to illustrate toString() 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 Month class Field[] fields = Month.class.getFields(); for (int i = 0; i < fields.length; i++) { // print name of Fields System.out.println("toString of Field:\n" + fields[i].toString()); } } } |
toString of Field:
public static final java.time.Month java.time.Month.JANUARY
toString of Field:
public static final java.time.Month java.time.Month.FEBRUARY
toString of Field:
public static final java.time.Month java.time.Month.MARCH
toString of Field:
public static final java.time.Month java.time.Month.APRIL
toString of Field:
public static final java.time.Month java.time.Month.MAY
toString of Field:
public static final java.time.Month java.time.Month.JUNE
toString of Field:
public static final java.time.Month java.time.Month.JULY
toString of Field:
public static final java.time.Month java.time.Month.AUGUST
toString of Field:
public static final java.time.Month java.time.Month.SEPTEMBER
toString of Field:
public static final java.time.Month java.time.Month.OCTOBER
toString of Field:
public static final java.time.Month java.time.Month.NOVEMBER
toString of Field:
public static final java.time.Month java.time.Month.DECEMBER
References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#toString–
Recommended Posts:
- Field set() method in Java with Examples
- Field get() method in Java with Examples
- Field getBoolean() method in Java with Examples
- Field setInt() method in Java with Examples
- Field equals() method in Java with Examples
- Field getFloat() method in Java with Examples
- Field getLong() method in Java with Examples
- Field getChar() method in Java with Examples
- Field getDeclaringClass() method in Java with Examples
- Field getInt() method in Java with Examples
- Field getName() method in Java with Examples
- Field getShort() method in Java with Examples
- Field setDouble() method in Java with Examples
- Field setByte() method in Java with Examples
- Field setChar() method in Java with Examples
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.

