El método getType() de java.lang.reflect.Field utilizado para obtener el tipo declarado del campo representado por este objeto Field. Este método devuelve un objeto Class que identifica el tipo declarado
Sintaxis:
public String getType()
Parámetros: Este método no acepta nada.
Valor de retorno: este método devuelve un objeto Class que identifica el tipo declarado.
Los siguientes programas ilustran el método getType():
Programa 1:
// 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; } }
Producción:
Type is double Type is float
Programa 2:
// 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()); } } }
Producción:
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
Referencias: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getType–
Publicación traducida automáticamente
Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA