nullpointer nullpointer - 22 days ago 5
Java Question

End of list of enums in Java

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?

Answer

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