What are enums and why are they useful?

后端 未结 27 1869
一整个雨季
一整个雨季 2020-11-22 07:06

Today I was browsing through some questions on this site and I found a mention of an enum being used in singleton pattern about purported thread safety benefits

27条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 07:32

    Java lets you restrict variable to having one of only a few predefined values - in other words, one value from an enumerated list. Using enums can help to reduce bug's in your code. Here is an example of enums outside a class:

    enums coffeesize{BIG , HUGE , OVERWHELMING }; 
    //This semicolon is optional.
    

    This restricts coffeesize to having either: BIG , HUGE , or OVERWHELMING as a variable.

提交回复
热议问题