position1
and position2
are static
, and on the Main
class. That means they're global there's just one version of each field for anyone who uses Main
. What you want are instance fields on Direction
. There's more info at the Java tutorial on member fields.
private enum Direction {
public int position1, position2;
...
}
You need to remove the static
keyword, so that each Direction
has its own position1
and position2
values. You may also want to mark them as final
, which means they can only be set once (that's a good general practice, for various reasons: makes it easier to reason about state, catches bugs like this where you accidentally overwrite values, makes for thread-safe code, etc).
When you access them, you need to specify which instance you want to get the position for:
d.position1
(instead of just position1
)
The reason this happens is that when you first use the Direction
enum, the JVM loads each of its four values. To do that, each value runs its constructor. For instance, when it loads North
it runs North(1, 1)
which sets position1 = 1; position2 = 1
. Then, when it loads South
it runs South(-1, -1)
, which then sets position1 = -1; position2 = -1;
. Since there's only one version of each field (that's what static
does), this South
constructor simply overwrites the values that the North
constructor wrote. The end result is that you'll get the position1
and position2
values for whichever constructor ran last. Since the enum's values are loaded in the order they appear in the code, that constructor is the one for the last value: East
.