- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Enumeration
An enumeration is a list of named constants. Even though Java offered some other features that provide somewhat similar functionality, like final variables, many programs still missed the conceptual purity of the enumerations, especially because enumerations are supported by lots other commonly used computer languages.
In their simplest form, Java enumerations appear similar to enumerations in the other computer languages. Nevertheless, this similarity may be only skin deep because, an enumeration defines a class type in Java. By making enumerations into classes, the capabilities of enumeration are greatly expanded. For instance, in Java, an enumeration can have methods, constructors, and instance variables. Therefore, although enumerations were several years in making. Java's rich implementation made them well worth the wait.
Create Enumeration in Java
An enumerations is created using the keyword enum. For example, following is a simple enumeration that lists the various apple varieties:
// here is an enumeration of several apple varieties enum Apple { GodenDel, Winesap, Jonathan, Cortland }
The identifiers GodenDel, Winesap, and so on, are called as enumeration constants. Each is absolutely declared as public, static final member of the Apple. Moreover, their type is the type of enumeration in which they are declared, that is Apple in this case. Therefore, in Java, these constants are self-typed, in which "self" thinks of to the enclosing enumeration.
Declare Enumeration Variable in Java
Once you have defined an enumeration, you can create a variable of that type. Nevertheless, even though enumerations define a class type, you do not instantiate an enum using the new operator/keyword. Instead, you can declare and use an enumeration variable in much the same way as you do one of the primitive types. For instance, this declares apl as a variable of enumeration the type Apple :
Apple apl;
Assign Value to Enumeration Variable in Java
Because apl is of the type Apple, the only values that it can be assigned or can contain are those defined by enumeration. For instance, following assigns apl the value GoldenDel constant :
apl = Apple.GoldenDel;
Notice that the symbol GoldenDel is preceded by Apple.
How to Compare two Enumeration Constants ?
The two enumeration constants can be compared for equality by using the == relational operator. For instance, this statement compares the value present in apl with the RedDel constant:
if(apl == Apple.RedDel) // ...
An enumeration value can also be used to control the switch statement. Naturally, all of the case statements must use the constants from the same enum as that used by the switch expression. For instance, the following switch is perfectly valid :
/* use an enum to control the switch statement */ switch(apl) { case GoldenDel: // ... case Cortland: // ...
Notice here that in the case statements, the names of the enumeration constants are used without being qualified by their enumeration type name, that is, Cortland is used, not Apple.Cortland. This is because the type of enumeration in the switch expression has already specified the enum type of the case constants. There is no need to qualify the constants in case statements with their enum type name. In fact, attempting to do so will cause a compilation error.
When an enumeration constant is displayed, such as in a statement println(), its name is output. For example, given this statement :
System.out.println(Apple.Cortland);
the name Cortland is displayed.
Java Enumeration Example
Here this program puts together all of the pieces and illustrates the Apple enumeration :
/* Java Program Example - Java Enumeration Example * An enumeration of several apple varieties */ enum Apple { GoldenDel, Winesap, RedDel, Jonathan, Cortland } class EnumerationDemo { public static void main(String args[]) { Apple apl; apl = Apple.GoldenDel; /* output an enum value */ System.out.println("Value of apl : " + apl); System.out.println(); apl = Apple.RedDel; /* compare the two enum values */ if(apl == Apple.RedDel) System.out.println("apl contains RedDel\n"); /* use an enum to control the switch statement */ switch(apl) { case GoldenDel : System.out.println("Golden Delicious is yellow"); break; case Winesap : System.out.println("Winesap is red"); break; case RedDel : System.out.println("Red Delicious is red"); break; case Jonathan : System.out.println("Jonathan is red"); break; case Cortland : System.out.println("Cortland is red"); break; } } }
When the above Java program is compile and run, it will produce the following output:
« Previous Tutorial Next Tutorial »