Menu
Topics Index
...
`

Compiler throwing an error with enum declared inside main method223 - Java Example Program


Compiler throwing an error with enum declared inside main method223
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.

© meritcampus 2019

All Rights Reserved.

Open In App