Java Reference
In-Depth Information
case MEDIUM:
days = 15;
break;
case HIGH:
days = 7;
break;
case URGENT:
days = 1;
break;
}
return days;
}
}
Associating Data and Methods to Enum Constants
Generally, you declare an enum type just to have some enum constants, as you have done in the Severity enum type.
Since an enum type is actually a class type, you can declare pretty much everything inside an enum type body that
you can declare inside a class body. Let's associate one data element, projected turnaround days, with each of your
Severity enum constants. You will name your enhanced Severity enum type as SmartSeverity . Listing 18-3 has
code for the SmartSeverity enum type, which is very different from the code for the Severity enum type.
Listing 18-3. A SmartSeverity enum Type Declaration, Which Uses Fields, Constructors, and Methods
// SmartSeverity.java
package com.jdojo.enums;
public enum SmartSeverity {
LOW(30), MEDIUM(15), HIGH(7), URGENT(1);
// Declare an instance variable
private int projectedTurnaroundDays;
// Declare a private constructor
private SmartSeverity(int projectedTurnaroundDays) {
this.projectedTurnaroundDays = projectedTurnaroundDays;
}
// Declare a public method to get the turnaround days
public int getProjectedTurnaroundDays() {
return projectedTurnaroundDays;
}
}
Let's discuss the new things that are in the SmartSeverity enum type.
It declares an instance variable
projectedTurnaroundDays , which will store the value of the
projected turnaround days for each enum constant.
// Declare an instance variable
private int projectedTurnaroundDays;
 
Search WWH ::




Custom Search