Monday, November 18, 2024
Google search engine
HomeLanguagesJavaField toString() method in Java with Examples

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


Output:

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


Output:

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–

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments