23

I was trying out enum type in Java. When I write the below class,

public class EnumExample {
  public enum Day {
    private String mood;
    MONDAY, TUESDAY, WEDNESDAY;
    Day(String mood) {

    }
    Day() {

    }
  }
 }

Compiler says: Syntax error on token String, strictfp expected.
I do know what's strictfp but would it come here?

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
Anoop Dixith
  • 633
  • 2
  • 8
  • 20
  • 2
    That's eclipse at its finest :D `strictfp` has nothing to do with strings, but with floating point arithmetic: http://stackoverflow.com/questions/517915/when-should-i-use-the-strictfp-keyword-in-java . – Svetlin Zarev Feb 03 '15 at 18:33
  • Eclipse will also give the same error, if you accidentally put a whitespace instead of an underscore in your constant declaration name. Like 'MONDAY_INT VALUE' – jumps4fun Aug 28 '18 at 08:23

3 Answers3

37

You have maybe forgotten to add semicolon after last enum constant.

public enum Element {
    FIRE,
    WATER,
    AIR,
    EARTH,  // <-- here is the problem

    private String message = "Wake up, Neo";
}
Firzen
  • 1,909
  • 9
  • 28
  • 42
  • 1
    That's correct. And this bites you when mocking enums where you don't even have enum values. The solution then is to add the semicolon anyway, as in: private static enum EmptyEnum implements EnumKey { ; @Override public String key() { return null; } } – Jim Showalter Oct 15 '15 at 00:57
  • that's cheeky :) – Maulik Kayastha Jan 17 '21 at 11:56
25

The enum constants must be first in the enum definition, above the private variable.

Java requires that the constants be defined first, prior to any fields or methods.

Try:

public enum Day {
    MONDAY, TUESDAY, WEDNESDAY;
    private String mood;
    Day(String mood) {

    }
    Day() {

    }
  }
rgettman
  • 176,041
  • 30
  • 275
  • 357
1

You can't define instance variable before enum elements/attributes.

public enum Day {
   
    MONDAY("sad"), TUESDAY("good"), WEDNESDAY("fresh");
    private String mood;
    Day(String mood) {
    this.mood = mood;
 }
Anil Nivargi
  • 1,473
  • 3
  • 27
  • 34
Ritunjay kumar
  • 261
  • 3
  • 6