2

I have one Enum like Below

public enum Game {
    CRICKET("cricket"),
    FOOTBALL("football"),
    VOLLEYBALL("volleyball")'

    private String val;

    private Game(String val) {
        this.val = val;
    }

    public String getValue() {
        return this.val;
    }
}

In here, Do I want to overide the equal(),hashCode(),toString() methods based on this What issues should be considered when overriding equals and hashCode in Java?

  • Enum values being constants, why would you need `equals()` and `hashcode()` ? – Arnaud Oct 23 '19 at 06:46
  • The only problem, better use `private final String val;` (or `public`). – Joop Eggen Oct 23 '19 at 07:10
  • can I know? why did you suggest like above @JoopEggen –  Oct 23 '19 at 11:32
  • You are talking about correct semantics, possible problems. There are not any, but `Game.FOOTBAL.val` should simply be a constant, and then (if so desired) it could also be public, as nobody could do `Game.FOOTBALL = "socker";`. That would make the class 100% semantically correct. _Besides the `)'` a typo for `);`_. – Joop Eggen Oct 23 '19 at 12:59

3 Answers3

6

Question is meaningless, because you cannot override equal() and hashCode() of an enum.

That is because they are defined as final in the Enum class, which implicitly is the base class of all enum types.

Andreas
  • 138,167
  • 8
  • 112
  • 195
  • What about toString method? –  Oct 23 '19 at 11:31
  • @cvaram96 As the javadoc of Enum's [`toString()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html#toString--) says: *This method may be overridden, though it typically isn't necessary or desirable. An enum type should override this method when a more "programmer-friendly" string form exists.* – Andreas Oct 23 '19 at 14:39
2

No, enum is not a regular class (and as Andreas pointed out, they're final methods). For example, you don't need to use equals() to compare them, as there is a single instance of each enum value. This allows you to use == instead.

if(gameType == Game.CRICKET)

You can override toString(), as by default it prints the same as name(), which is just the name of the enum (e.g. FOOTBALL).

Kayaman
  • 67,952
  • 3
  • 69
  • 110
0

My first question is why you would need the enum to contain a String in the first place. The point of your enum already looks like it is to differentiate each type of game within a certain list of possible games. So the enum itself here functions as a constant value. For example you could do:

public void playGame( Game myGame ){
  switch(myGame){
    case FOOTBALL: 
      playFootball();
      break;
    case CRICKET:
      playCricket();
      break;
   // and so on but you get the point
}
George Weekson
  • 484
  • 3
  • 12
PreciseMotion
  • 315
  • 1
  • 11
  • As the [javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html#toString--) says: *This method may be overridden, though it typically isn't necessary or desirable. An enum type should override this method when **a more "programmer-friendly" string** form exists.* --- So that's why. – Andreas Oct 23 '19 at 06:58