Reputation: 4025
Why is it that when I define an enum, I pass it a list of field names, and then somehow those field names (e.g. Days.MONDAY) end up referring to field values? I can pass along a field (e.g. Days.MONDAY) and then use a switch to get the field value. Even more strange, when I declare the enum fields, I don't even have to put the names in quotes, even though they are actually values.
Upvotes: 1
Views: 1425
Reputation: 88378
Think of the Java enum as a nice syntax for defining a class. Here is a shell transcript that might help:
$ cat > Direction.java
public enum Direction {NORTH, EAST, SOUTH, WEST}
$ javac Direction.java
$ javap Direction
Compiled from "Direction.java"
public final class Direction extends java.lang.Enum{
public static final Direction NORTH;
public static final Direction EAST;
public static final Direction SOUTH;
public static final Direction WEST;
public static Direction[] values();
public static Direction valueOf(java.lang.String);
static {};
}
So yes, you can say EAST
is a field of the class Direction
and its value is an instance of class Direction
. We refer to this value as Direction.EAST
the same way you refer to a value through a static field of any other class.
Perhaps the confusion is a result of the fact that you don't see a declaration like
public static final Direction.EAST = SOMETHING_OR_OTHER_HERE;
This is understandable. Enums are designed to initialize these field values, but you don't have to explicitly initialize them yourself. It is as if you said
public static final Direction.EAST = new Direction();
which is, as a matter of fact, a part of what is called the "Typesafe Enum" pattern which was commonly used before Java got the current enum syntax.
Regarding your comment about quotes, there is simply no reason to have to quote anything, because there are no strings involved.
Regarding your comment about switches, yes, it is somewhat interesting how the notion of field name and field value play out here. But that is only because the value looks like the field. In other words, the value of field EAST
in Direction
is, well, Direction.EAST
. If you think about it, though, it is not too different from literals. What is the value of Float.NaN
you may ask? Well, it is.... Float.NaN
. Kind of an opaque value.
Upvotes: 3
Reputation: 2674
Enums in Java aren't like enums in other languages. They take some getting use to. Essentially, an enum is a special class, and each enum you declare is an object of that class:
public enum Day {
MONDAY, TUESDAY, WEDNESDAY
}
MONDAY, TUESDAY, and WEDNESDAY are objects of the class Day, and you get pretty much the same effect using normal classes, but the enum syntax is explicit about what your intention is. Also, you can't create THURSDAY somewhere else -- only inside the enum braces. That means that the compiler always knows the complete list of enum values, and that allows Java to use enums in such places as switch statements, since the compiler has all the information necessary to create a jump table and to check for the correctness of your code.
So MONDAY is not a label for some integer value, like enums in other languages. It is an object like any other Java object. That's why you don't put them in quotes. It's like writing:
final static Day MONDAY = new Day();
final static Day TUESDAY = new Day();
and so on. You can even do tricks like this:
public enum Day {
MONDAY("Mon", false), TUESDAY("Tue", false), WEDNESDAY("Wed", true);
private String shortForm = null;
private boolean humpDay = false;
Day(String shortForm, boolean humpDay) {
this.shortForm = shortForm;
this.humpDay=humpDay;
}
public String getShortForm() {
return shortForm;
}
public boolean isHumpDay() {
return humpDay;
}
}
Each of MONDAY, TUESDAY, WEDNESDAY are objects of Day, and each has it's own members, include shortForm and humpDay. And there is even a constructor. See? Objects, not values.
Need the short form for TUESDAY? Just do TUESDAY.getShortForm(). You can't do that with your relabeled-integer-constant paradigm in other languages.
I hope that helps. Java enums are exceptionally powerful, but for people used to enums from other languages, it can be confusing until they have that breakthrough moment ("I see it now! They're like object declarations!")
Upvotes: 5
Reputation: 4280
The way I perceive enum is that it is just a "list" of unique values that has aliases assigned to them - names. For days you could have array of integers from 1 to 7 but in your code it is much more convenient to have names assigned to sort of things that represent objects ie Monday, Tuesday.. etc. You could as well have constants declared for each day of the week but you would have to type more code. enum doesn't seem to introduce some new programming paradigm like class does, just a convenience shortcut tool.
Upvotes: -1