Field getType() method in Java with Examples

The getType() method of java.lang.reflect.Field used to get the declared type of the field represented by this Field object.This method returns a Class object that identifies the declared type

Syntax:

public String getType()

Parameters: This method accepts nothing.

Return value: This method returns a Class object that identifies the declared type.

Below programs illustrate getType() method:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate getType() method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws Exception
    {
  
        // Get the marks field object
        Field field = User.class.getField("Marks");
  
        // Apply getType Method on User Object
        // to get the Type of Marks field
        Class value = field.getType();
  
        // print result
        System.out.println("Type"
                           + " is " + value);
  
        // Now Get the Fees field object
        field = User.class.getField("Fees");
  
        // Apply getType Method on User Object
        // to get the Type of Fees field
        value = field.getType();
  
        // print result
        System.out.println("Type"
                           + " is " + value);
    }
}
  
// sample User class
class User {
  
    // static double values
    public static double Marks = 34.13;
    public static float Fees = 3413.99f;
  
    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;
    }
}

chevron_right


Output:

Type is double
Type is float

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate getType() 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("Name of Field: "
                               + fields[i].getType());
        }
    }
}

chevron_right


Output:

Name of Field: class java.time.Month
Name of Field: class java.time.Month
Name of Field: class java.time.Month
Name of Field: class java.time.Month
Name of Field: class java.time.Month
Name of Field: class java.time.Month
Name of Field: class java.time.Month
Name of Field: class java.time.Month
Name of Field: class java.time.Month
Name of Field: class java.time.Month
Name of Field: class java.time.Month
Name of Field: class java.time.Month

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getType–

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.