Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6428695

No compiler error if enum has a method that is override-equivalent to compareTo(Object)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P2 P2
    • None
    • 6
    • tools
    • generic
    • generic

      There is an JLS3 statement (8.9 Enums):
      "It follows that enum type declarations cannot contain fields that conflict with the enum constants, and cannot contain methods that conflict with the automatically generated methods (values() and valueOf(String)) or methods that override the final methods in Enum: (equals(Object), hashCode(), clone(), compareTo(Object), name(), ordinal(), and getDeclaringClass())."

      As I understand, javac should report error, if we define such fields or methods.
      Actually it does report compile-time error for all these, except compareTo(Object).

      For example:

      enum TestEnum {
          a1, a2, a3;
          public void compareTo(Object obj) {
          }
      }

      no problem is reported by javac.

            ahe Peter Ahe
            vrudomet Victor Rudometov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: