I have an interface
and an enum
I am implementing the interface
in the enum
I am using eclipse
as my IDE
Following is my interface
file -
ITrafficSignal.java
-->
public interface ITrafficSignal {
public void showAction();
}
Following is my enum
file -
ETrafficSignal.java
-->
public enum ETrafficSignal implements ITrafficSignal {
;
@Override
public void showAction() {
}
}
here the ;
in line 2 is being automatically being put by eclipse
If I remove ;
, then in the method
being implemented
-
public void showAction()
is having a red line below the keyword
void
and the error says -
Multiple markers at this line
- implements ITrafficSignal.showAction
- Syntax error on token "void", volatile expected
I am unable to understand this.
That's simply the syntax of enum types. From §8.9 Enum Types of the Java Language Specification:
EnumDeclaration: {ClassModifier} enum TypeIdentifier [Superinterfaces] EnumBody
And the syntax of EnumBody
is:
EnumBody: { [EnumConstantList] [,] [EnumBodyDeclarations] } EnumConstantList: EnumConstant {, EnumConstant} EnumConstant: {EnumConstantModifier} Identifier [( [ArgumentList] )] [ClassBody] EnumConstantModifier: Annotation
And finally the syntax of EnumBodyDeclarations
is:
EnumBodyDeclarations: ; {ClassBodyDeclaration}
As you can see, the above shows that the enum body declarations must be preceded by a semicolon (;
) separating it from the enum constants. Unlike the EnumConstantList
the semicolon is not optional1. This means the semicolon must be present if there's enum body declarations regardless of there being any enum constants declared, though I personally don't see the point in having an enum type without constants.
1. The entire EnumBodyDeclarations
is optional, however, meaning if there's no body declarations then the semicolon can be omitted.