While implementing an interface in an enum, why eclipse automatically puts a semicolon before the interface method being overridden?

acidic_base :

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.

Slaw :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=299719&siteId=1