El método isEnumConstant() de java.lang.reflect.Field se usa para verificar si este campo representa un elemento de un tipo enumerado o no. Si este campo representa un elemento de un tipo enumerado, el método devuelve verdadero o falso. Sintaxis:
public boolean isEnumConstant()
Parámetros: Este método no acepta nada. Retorno : este método devuelve verdadero si y solo si este campo representa un elemento de un tipo enumerado. Los siguientes programas ilustran el método isEnumConstant(): Programa 1:
Java
// Java program to illustrate // isEnumConstant () method import java.lang.reflect.Field; import java.time.Month; public class GFG { public static void main(String[] args) throws NoSuchFieldException { // get all declared fields of Month class Field[] declaredFields = Month.class.getDeclaredFields(); // loop through Field array // and print Field name and // check whether the field is EnumConstant for (int i = 0; i < declaredFields.length; i++) { System.out.print( "Field --> Name: " + declaredFields[i].getName() + "; isEnumConstant: " + declaredFields[i] .isEnumConstant() + "\n"); } } }
Producción:
Field --> Name: JANUARY; isEnumConstant: true Field --> Name: FEBRUARY; isEnumConstant: true Field --> Name: MARCH; isEnumConstant: true Field --> Name: APRIL; isEnumConstant: true Field --> Name: MAY; isEnumConstant: true Field --> Name: JUNE; isEnumConstant: true Field --> Name: JULY; isEnumConstant: true Field --> Name: AUGUST; isEnumConstant: true Field --> Name: SEPTEMBER; isEnumConstant: true Field --> Name: OCTOBER; isEnumConstant: true Field --> Name: NOVEMBER; isEnumConstant: true Field --> Name: DECEMBER; isEnumConstant: true Field --> Name: ENUMS; isEnumConstant: false Field --> Name: $VALUES; isEnumConstant: false
Programa 2:
Java
// Java program to illustrate // isEnumConstant () method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws NoSuchFieldException { // get all declared fields of Symbols class Field[] declaredFields = Symbols.class.getDeclaredFields(); // loop through Field array // and print Field name and // check whether the field is EnumConstant for (int i = 0; i < declaredFields.length; i++) { System.out.print( "Field --> Name: " + declaredFields[i].getName() + "; isEnumConstant: " + declaredFields[i] .isEnumConstant() + "\n"); } } private static enum Symbols { Alpha, Beta, Gamma } }
Producción:
Field --> Name: Alpha; isEnumConstant: true Field --> Name: Beta; isEnumConstant: true Field --> Name: Gamma; isEnumConstant: true Field --> Name: ENUM$VALUES; isEnumConstant: false
Referencias: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#isEnumConstant–
Publicación traducida automáticamente
Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA