Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End of list of enums in Java

Tags:

java

syntax

enums

I have a code block as follows :

  1. public enum TierEnum {
        Express,
        Standard; // the semi-colon is redundant
    }
    

Well in the code semi-colon(;) is marked as redundant by the compiler. At the same time if I use

  1. public enum TierEnum {
        Express,
        Standard 
    }; // again the semi-colon is redundant
    

Why in both the cases is the semi-colon marked redundant? How do I define the end of the list of enums in Java?

like image 836
Naman Avatar asked Dec 11 '22 16:12

Naman


1 Answers

The terminating semicolon is needed if you add some code to the enum, like:

public enum TierEnum
{
    Express( "Exp"),
    Standard( "Std");

    private String abbr;

    private TierEnum( String aAbbr )
    {
        abbr = aAbbr;
    }


    public String getAbbr()
    {
        return abbr;
    }
}
like image 135
Heri Avatar answered Dec 31 '22 07:12

Heri