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 enum IPLTeams
enum Move enum Move Accessing an enum constant using enum name
enum Move 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.
Compiler throwing an error with enum declared inside main method CODE class EnumDemo 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
|