Field isEnumConstant() method in Java with Examples

The isEnumConstant() method of java.lang.reflect.Field used to check if this field represents an element of an enumerated type or not. If this field represents an element of an enumerated type then method returns true else false.

Syntax:

public boolean isEnumConstant()

Parameters: This method accepts accepts nothing.



Return: This method returns true if and only if this field represents an element of an enumerated type.

Below programs illustrate isEnumConstant () method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
        }
    }
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Field --> Name: Alpha; isEnumConstant: true
Field --> Name: Beta; isEnumConstant: true
Field --> Name: Gamma; isEnumConstant: true
Field --> Name: ENUM$VALUES; isEnumConstant: false

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.