XyzWs Java FAQ: What can or can not for the Java enum type when you use it?
What can or can not for the Java enum type when you use it?
An enum is a special kind of class. The Java Language Specifcation said "There are two kinds of class declarations - normal class declarations and enum declarations".
An enum declaration specifies a new named enum type. The body of an enum type may contain enum constants. An enum constant defines an instance of the enum type. An enum type has no instances other than those defined by its enum constants (8.9 Enums in Java Language Specification).
An enum can be declared outside or inside a class, but can not be declared in a method. Only public or default modifier can be used by the declaration of a top-level enum. You can use public, default, protected or private modifier on the declaration of a nest enum.
All of enum type are subclasses of java.Lang.Enum, since Java does not support multiple inheritance, an enum cannot extend anything else.
The constructor of java.Lang.Enum is protected.
The enum variables are join serialazation because java.Lang.Enum implements Serializable and Comparable interfaces.
The java.Lang.Enum provides final methods (see Enum API Doc): name, compareTO, equals, hashCode, ordinal, clone, and getDeclaringClass. The final clone method in Enum ensures that enum constants can never be cloned. An enum type that contains constant-specific class bodies cannot override final enum methods.
You can overide toString() method in . The default toString() returns the name of this enum constant, as contained in its enum declaration. An enum type should override this method when a more "programmer-friendly" string form exists.
enum CarColor {
RED, GREEN, BLUE;
static public void main(String[] args) {
for (CarColor c : CarColor.values())
out.print(c + " ");
}
}
The output result is REDGREENBLUE.
enum CarColor {
RED("red"), GREEN("green"), BLUE("blue");
String color;
CarColor(String color)
{
this.color = color;
}
public String toString() { return "The car has "+ color + " color.";}
static public void main(String[] args) {
for (CarColor c : CarColor.values())
out.println(c);
}
}
The output result is
The car has red color.
The car has green color.
The car has blue color.
In addition, each enum type has the following implicitly declared two static methods (they are automatically generated methods):
Static method values() that returns an array containing the constants of the enum type, in the order thet are declared.
Static method valueOf(String) that returns the appropriate enum for the string passed in.
An enum class can have constructors but you can not explicitly instantiate an enum type because only private modifier allows for its constructors. All constructors of an enum class are implicitly private, even if the private modifier are omitted.
A defult constructor is created if no constructors are provided for the enum type. Again, This default constructor is private.
Each enum constant implicts The constructors will be invoked by the enum constants in the order of their declared in the enum type.
The enum type is a special class so you can add constructors, instance variables and methods.
But it is a compile-time error for an enum to declare a finalizer. An instance of an enum may never be finalized.
The enum constants must be declared before any other variables and methods declaration in an enum type. If an enum declares only values in it, then semi-colon is optional. Otherwise, the semi-colon at the end of declared values is mandatory. For example, you need ";" at the end of BLUE("blue").
An enum class can have "public static void main(String[] args)" and run as standalone application.
When enum outside the class is accessed, fully qualified name must be used.
enum CarColor {
RED("red"), GREEN("green"), BLUE("blue");
String color;
CarColor(String color)
{
this.color = color;
}
public String toString() { return "The car has "+ color + " color.";}
//public static void main(String[] args) {
// CarColor c = CarColor.RED;
// out.print(c);
//}
}
public class Program {
public static void main(String[] args) {
CarColor c = CarColor.RED;
//CarColor c = RED; //this is simple name compiler error
out.print(c);
}
}
Constant-specific class bodies define anonymous classes inside an enum type that extend the enclosing enum type. For example,
enum CarColor {
RED("red") { public String toString() { return "This is a "+ red +"color car."; }},
GREEN("green"),
BLUE("blue");
String color;
CarColor(String color)
{
this.color = color;
}
public String toString() { return "The car has "+ color + " color.";}
static public void main(String[] args) {
for (CarColor c : CarColor.values())
out.println(c);
}
}
You can override some of enum methods but you can not override final enum methods.
An enum type can have abstract enum methods but you must have constant-specific class bodies for every enum constant, and any abstract methods declared in the enum class body declarations are overridden in all the constant-specific class bodies. Here is an example for Java Language Specification:
import java.util.*;
public enum Operation {
PLUS {
double eval(double x, double y) { return x + y; }
},
MINUS {
double eval(double x, double y) { return x - y; }
},
TIMES {
double eval(double x, double y) { return x * y; }
},
DIVIDED_BY {
double eval(double x, double y) { return x / y; }
};
//Perform the arithmetic operation represented by this constant
abstract double eval(double x, double y);
public static void main(String args[]) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
for (Operation op : Operation.values())
System.out.println(x + " " + op + " " + y + " = " + op.eval(x, y));
}
}
or you will have compiler error:
import java.util.*;
public enum Operation {
PLUS {
double eval(double x, double y) { return x + y; }
},
MINUS,
TIMES,
DIVIDED_BY;
//Perform the arithmetic operation represented by this constant
abstract double eval(double x, double y);
public static void main(String args[]) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
for (Operation op : Operation.values())
System.out.println(x + " " + op + " " + y + " = " + op.eval(x, y));
}
}
In the constant-specific class bodies for an enum constant, you can not overload methods. You will get compiler error. For example,
enum CarColor {
RED("red") { public String toString(String s) { return s + " This is a "+ red +"color car."; }},
GREEN("green"),
BLUE("blue");
String color;
CarColor(String color)
{
this.color = color;
}
public String toString() { return "The car has "+ color + " color.";}
static public void main(String[] args) {
for (CarColor c : CarColor.values())
out.println(c);
}
}
How do I access an enum type that is delcared inside another class?
class Program {
enum CarColor {
RED("red"),
GREEN("green"),
BLUE("blue");
String color;
CarColor(String color)
{
this.color = color;
}
public String toString() { return "The car has "+ color + " color.";}
}
}
public class MyProgram {
public static void main(String[] args) {
Program.CarColor c = Program.CarColor.RED;
}
}