Field toString() method in Java with Examples - GeeksforGeeks

26 Aug, 2019
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 classclass 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.JANUARYtoString of Field:public static final java.time.Month java.time.Month.FEBRUARYtoString of Field:public static final java.time.Month java.time.Month.MARCHtoString of Field:public static final java.time.Month java.time.Month.APRILtoString of Field:public static final java.time.Month java.time.Month.MAYtoString of Field:public static final java.time.Month java.time.Month.JUNEtoString of Field:public static final java.time.Month java.time.Month.JULYtoString of Field:public static final java.time.Month java.time.Month.AUGUSTtoString of Field:public static final java.time.Month java.time.Month.SEPTEMBERtoString of Field:public static final java.time.Month java.time.Month.OCTOBERtoString of Field:public static final java.time.Month java.time.Month.NOVEMBERtoString 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–
Java-Field
Java-Functions
java-lang-reflect-package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Hashtable in Java
Constructors in Java
Different ways of Reading a text file in Java
Comparator Interface in Java with Examples
Java Math random() method with Examples
HashMap containsKey() Method in Java
How to Create Array of Objects in Java?
Convert Double to Integer in Java
Iterating over ArrayLists in Java
Generating random numbers in Java