Menu
Topics Index
...
`


Exploring java.lang >
Siva Nookala - 13 Sep 2016
Enumerations, in general, are a set of named constants. They have been in other programming languages from the beginning (e.g., C, C++). Prior to Java 1.5, Java lacked language support for enumerations. This concept was added in 1.5v and the enum in Java is more powerful than many other languages.

For example:
enum Move
{
    LEFT,
    RIGHT,
    UP,
    DOWN;    // ; is optional
}
enum  IPLTeams
{
    SRH, MI, RCB, CSK, DD, KXIP, RR, KKR;
}
Internal implementation of enum:
  • enum is internally implemented using the concept of class.
  • Every enum constant is a reference variable to that enum type object.
  • Every enum constant is always public, static and final.
enum Move
{
    LEFT,
    RIGHT,
    UP,
    DOWN;                
}
This declaration is equivalent to:
enum Move
{
    public static final Move LEFT = new Move();
    public static final Move RIGHT = new Move();
    public static final Move UP = new Move();
    public static final Move DOWN = new Move();
}
Declaration and usage of enum: As we know, every enum constant is always static. Hence we can access it using enum name. Example: Accessing an enum constant using enum name
enum Move
{
    LEFT,    
    RIGHT,
    UP,
    DOWN;                
}
public class EnumTest
{
    public static void main( String[] args )
    {
                Move m1 = Move.UP;   // LINE A
                System.out.println( m1 );
    }  
}
Output: UP
Here we are accessing the enum constant UP using the enum name Move (LINE A).
NOTE: In enum, toString() is implicitly overridden to return its name.

Where can we declare an enum?
enum can be declared either within a class or outside of a class but not inside a method.
Take a look at this example. Here the compiler throws an error indicating that enum types cannot be local.

Method Description
equals(Object obj)
Returns true if obj and the invoking object refer to the same constant.
name()
Returns the unaltered name of the invoking constant.
toString()
Returns the name of the invoking constant.This name may differ from the one used in enumeration's declaration.
ordinal()
Returns a value that indicates an enumeration constant's position in the list of constants.
valueOf(Class T e-type, String name)
Returns the constant associated with name int the enumeration type specified by e-type.
Compiler throwing an error with enum declared inside main method
class EnumDemo
{
    public static void main(String arg[])
    {
        enum Move
        {
            LEFT, RIGHT, UP, DOWN;
        }
                
        Move m1 = Move.UP;
        System.out.println( m1 );
        
    
    }
}
OUTPUT

Compilation Error: enum types must not be local.

DESCRIPTION

Here we have declared enum inside main. So, the compiler throws an error.

THINGS TO TRY
  • Consider the example Accesing an enum constant using enum name, try to access remaining enum constants as well in the same way.
  • Try to compile the same program by removing the semicolon after the last enum constant and observe that the semicolon is optional.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App