TL;DR
Solution to your problem
I suggest making the currentUpperBound()
method protected
.
Why?
You should consider each value of your enum as an anonymous subclass of Days
. Moreover, since the enum must guarantee each of its value is unique (so that you can test equality with ==
for instance), it is stored as a static final
field (hence also the habit of naming enum values with capital letters.
Understanding the enum behind the scene
Code equivalence
To show some code on how an enum works1:
public enum MyEnum {
JESSE {
@Override
public void sayMyName() {
System.out.println("Pinkman");
}
}, WALTER;
public void sayMyName() {
System.out.println("Heisenberg");
}
private void unreachableMethod() {
// try again
}
}
is (almost) equivalent to:
public class MyEnum {
private static final MyEnum JESSE = new MyEnum() {
@Override
public void sayMyName() {
System.out.println("Pinkman");
}
};
private static final MyEnum WALTER = new MyEnum();
public void sayMyName() {
System.out.println("Heisenberg");
}
private void unreachableMethod() {
// try again
}
}
When you write it this way, some things become much easier to understand:
- why ==
works for testing enum equality
Only one pointer per value. Object
's equals(Object)
is perfect here as it tests only that.
- why you can inherit methods from MyEnum
or access private static
ones, but not private
non-static methods
- The values are stored as
static
fields so they cannot access instance context.
- The value inherit
MyEnum
so anything accessible through legacy is visible to them.
Why did I say that is was almost equivalent?
There are some additional controls embedded within the enum mechanism.
For instance, the switch
is able to tell whether you had a case
for each value for an enum: try testing only some values and not defining a default
, the compiler will raise a warning. This would be impossible if you were simply using an abstract class with constants.
The values()
method also shows things are more evolved than my simple example, but I think this helps understanding.
1 based on my understanding of the JSL