Why is Eclipse asking to declare strictfp inside enum

22k Views Asked by At

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?

3

There are 3 best solutions below

3
rgettman On

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() {

    }
  }
2
Firzen On

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";
}
0
Ritunjay kumar On

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;
 }