This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: Enum in java part 2
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
You can obtain an array of all the possible values of a Java enum type by calling its static values() method.
All enum types get a static values() method automatically by the Java compiler. Here is an example of iterating all values of an enum:
for (Day day : Day.values()) {
System.out.println(day);
}
If you run this code, then you will get the following output:
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
4. Enum fields:
It is possible to add fields to Java Enum. Each constant enum value gets these fields. The field values must be supplied to the constructor of the enum when defining the constants.
The example above has a constructor which takes an int. The enum constructor sets the int field. When the constant enum values are defined, an int value is passed to the enum constructor.
The enum constructor must be either private or package scope (default). You cannot use public or protected constructors for a Java enum.
5. Enum Methods:
It is possible to add methods to Java Enum. Here I am showing an example that how to do it.
It is possible to call an Enum method through a reference to one of the constant values. The following code will show you how to call Java Enum method.
Day day = Day.MONDAY;
System.out.println(day.getDayCode()); //which gives 2.Because the dayCode field for the Enum constant MONDAY is 2.
Some details to know about enum:
Java enums extend the java.lang.Enum class implicitly, so your enum types cannot extend another class. Please refer the following code. This code is from Java API.
public abstract class Enum<E extends Enum<E>> implements Comparable<E>,Serializable {
// Enum class code in Java API
}
If a Java enum contains fields and methods, the definition of fields and methods must always come after the list of constants in the enum. Additionally, the list of enum constants must be terminated by a semicolon;
Java does not support multiple inheritance, and enum also does not support for multiple inheritance.
Enums are type-safe. Enum has their own name-space. It means your enum will have a type for example “Day” in below example and you cannot assign any value other than specified in Enum Constants.
Enum constants are implicitly static and final and cannot be changed once created.
Enum can be safely compare using:
Switch-Case Statement
== Operator
.equals() method
You cannot create instance of enums by using new operator in Java because constructor of Enum in Java can only be private and Enums constants can only be created inside Enums itself.
Instance of Enum in Java is created when any Enum constants are first called or referenced in code
An enum can be declared outside or inside a class, but NOT in a method.
An enum declared outside a class must NOT be marked static, final , abstract, protected , or private
Enums can contain constructors, methods, variables, and constant class bodies.
Enum constructors can have arguments, and can be overloaded.
Enum constructors can have arguments, and can be overloaded.
The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself
package com.enum.exampleprogram;
/**This is an Enum which indicates possible days in a week **/