Singing the tune of working out the remaining kinks in my understanding of Java 5 goodies after employment that constrained me to writing only Java 1.4-compatible code (see Generics), let's see what we can do with enumerations. Here are a few reasonable examples to copy and modify. |
![]() |
Eclipse tip: Adding plug-ins to EclipseIt's easy to upgrade Eclipse to do lots of handy little things and important big things. For example, AnyEdit will remove dead, white space from your source code and perform other menial tasks such converting characters to HTML graphemes, variables with underscores to camel-back case or vice-versa. Pull down the Help menu, choose Install New Software..., then type http://andrei.gmxhome.de/eclipse/ into Work with:. Patiently await options to show up in the window in the middle. Check the triangle next to Eclipse 3.3 - 3.5 plugins and check the AnyEdit box. If you get a failure containing things like:
No repository found...
...then close Eclipse and relaunch thus: |
A very simple enumeration:
01.
public
enum
MuscleCarsOfThe1960s
02.
{
03.
Dodge_Charger,
04.
Plymouth_Barracuda,
05.
Plymouth_RoadRunner,
06.
Plymouth_SuperBird,
07.
Chevrolet_Chevelle_Malibu_SS,
08.
Chevrolet_Impala_SS,
09.
Oldsmobile_442,
10.
Pontiac_GTO,
11.
Ford_Thunderbolt,
12.
Ford_Mustang_Mach_I
13.
}
How to enumerate the values of a Java enumeration:
1.
for
( MuscleCarsOfThe1960s muscleCar : MuscleCarsOfThe1960s.values() )
2.
System.out.println( muscleCar );
—output looks like this:
Dodge_Charger Plymouth_Barracuda Plymouth_RoadRunner Plymouth_SuperBird Chevrolet_Chevelle_Malibu_SS Chevrolet_Impala_SS Oldsmobile_442 Pontiac_GTO Ford_Thunderbolt Ford_Mustang_Mach_I
Use an enumeration thus:
MuscleCarsOfThe1960s car = MuscleCarsOfThe1960s.Dodge_Charger; . . . if( car == MuscleCarsOfThe1960s.Oldsmobile_442 ) ...
A more complex enumeration that saves state for a purpose.
01.
public
enum
OperatingSystem
02.
{
03.
LINUX,
04.
WINDOWS,
05.
UNKNOWN;
06.
07.
private
static
OperatingSystem currentSystem = UNKNOWN;
08.
09.
public
static
OperatingSystem system()
10.
{
11.
if
( currentSystem == UNKNOWN )
12.
{
13.
String osname = System.getProperty(
"os.name"
);
14.
15.
if
( osname.startsWith(
"Linux"
) )
16.
currentSystem = LINUX;
17.
else
if
( osname.startsWith(
"Windows"
) )
18.
currentSystem = WINDOWS;
19.
else
20.
currentSystem = UNKNOWN;
21.
}
22.
23.
return
currentSystem;
24.
}
25.
}
The above enumeration is practical for uses such as:
if( OperatingSystem.system() == OperatingSystem.LINUX ) ...
As implied by the sample usage of the last example, you must use the type (here, OperatingSystem) when referring to any of the enumeration values, but, because of the type of the discriminator, this is not necessary in a switch statement. Assume the following is coded in a class that consumes enum OperatingSystem:
01.
.
02.
.
03.
.
04.
OperatingSystem system = OperatingSystem.system();
05.
06.
if
( system == LINUX )
// (syntax error!)
07.
...
08.
09.
if
( system == OperatingSystem.LINUX )
// (the correct way)
10.
...
11.
12.
switch
( system )
// (good because compiler understands 'system')
13.
{
14.
case
LINUX :
15.
System.out.println(
"This is Linux!"
);
16.
break
;
17.
case
WINDOWS :
18.
System.out.println(
"This is Windows!"
);
19.
break
;
20.
default
:
21.
case
UNKNOWN :
22.
System.out.println(
"The operating system is unknown!"
);
23.
break
;
24.
}
25.
.
26.
.
27.
.
Of course, you can always do the following and take advantage of the fact that the values can be integers.
01.
public
enum
Color
02.
{
03.
black (
0
),
04.
white (
1
),
05.
red (
2
),
06.
yellow(
3
),
07.
pink (
4
),
08.
green (
5
),
09.
orange(
6
),
10.
purple(
7
),
11.
blue (
8
);
12.
13.
private
int
color;
14.
15.
private
Color(
int
c ) {
this
.color = c; }
16.
public
int
getColor() {
return
this
.color; }
17.
18.
@Override
19.
public
final
String toString()
20.
{
21.
String string =
null
;
22.
23.
switch
(
this
)
24.
{
25.
case
black : string =
"black"
;
break
;
26.
case
white : string =
"white"
;
break
;
27.
case
red : string =
"red"
;
break
;
28.
case
yellow : string =
"yellow"
;
break
;
29.
case
pink : string =
"pink"
;
break
;
30.
case
green : string =
"green"
;
break
;
31.
case
orange : string =
"orange"
;
break
;
32.
case
purple : string =
"purple"
;
break
;
33.
case
blue : string =
"blue"
;
break
;
34.
}
35.
36.
return
string;
37.
}
38.
}
For a similar example, using Strings instead of integers, see here.
This next example illustrates some of the oblique power behind initialization of Java 5's enum type. With an object of JdbcEntity, entity, many expressions are possible that abbreviate and clarify JDBC operation code.
entity.getTableName(); entity.getReferenceColumn(); switch( entity ) { case User : . . . }
Etc. Here's the enum definition. Note that to do this (rather bizarre) sort of enumeration, you must have a (private) constructor that provides for its construction. Hence the constructor below isn't callable outside.
01.
public
enum
JdbcEntity
02.
{
03.
// authorization entity is a user, group, etc...
04.
User (
"t_attr_user"
,
"user_id"
),
05.
Group (
"t_attr_group"
,
"group_id"
),
06.
Role (
"t_attr_role"
,
"role_id"
),
07.
Resource(
"t_attr_rsrc"
,
"rsrc_id"
);
08.
09.
private
String tableName;
// table in which attributes will be stored
10.
private
String refColumn;
// reference column in the table
11.
12.
/**
13.
* Private constructor.
14.
*/
15.
private
JdbcEntity( String table, String column )
16.
{
17.
this
.tableName = table;
18.
this
.refColumn = column;
19.
}
20.
21.
public
String getTableName() {
return
this
.tableName; }
22.
public
String getReferenceColumn() {
return
this
.refColumn; }
23.
}
And, again, another example...
01.
public
enum
Genre
02.
{
03.
Action (
"Action"
),
04.
Adventure (
"Adventure"
),
05.
Allegory (
"Allegory"
),
06.
Cartoon (
"Cartoon"
),
07.
Chick (
"Chick flick"
),
08.
Comedy (
"Comedy"
),
09.
Drama (
"Drama"
),
10.
Fantasy (
"Fantasy"
),
11.
History (
"History"
),
12.
Military (
"Military"
),
13.
Music (
"Music"
),
14.
Musical (
"Musical"
),
15.
Mystery (
"Mystery "
),
16.
Politics (
"Politics"
),
17.
Religious (
"Religious"
),
18.
Science (
"Science fiction"
),
19.
Suspense (
"Suspense"
),
20.
Thriller (
"Thriller"
);
21.
22.
private
String genre =
null
;
23.
24.
private
Genre( String genre ) {
this
.genre = genre; }
25.
public
final
void
setGenre( String genre ) {
this
.genre = genre; }
26.
public
final
String getGenre() {
return
this
.genre; }
27.
}