compareTo and nested Enums
- by echox
Hi, in "A Programmers Guide to Java SCJP Certification" I found an example which I can't follow.
This the given enum:
enum Scale3 {
GOOD(Grade.C), BETTER(Grade.B), BEST(Grade.A);
enum Grade {A, B, C}
private Grade grade;
Scale3(Grade grade) {
this.grade = grade;
}
public Grade getGrade() { return grade; }
}
This is the given expression:
Scale3.GOOD.getGrade().compareTo(Scale3.Grade.A) > 0;
I don't understand why this expression will be true?
The return value will be 2.
compareTo() will return a value 0 if the given object is "less" than the object.
Scale3.Grade.A is the "biggest" element of Grades, its ordinal number is 0.
Scale3.GOOD is the "biggest" element of Scale3, its ordinal number is also 0.
The constructor of Scale3 is called with Scale3.Grade.C, which ordinal number is 2.
So the given expression is equal to the following code:
Scale3.Grade.C.compareTo(Scale3.Grade.A) > 0;
A is "bigger" than C, so shouldn't be the result < 0?