Java – what is enum useful?
•
Java
See English answers > What are enums and why are they useful? twenty-two
It is something between classes and variables, but I know where I can use it, so it will be a more useful class, or some variables, such as
Solution
There are other things you can do and why you use them, but the main reason I use enumeration is to prevent the possibility of invalid parameters For example, imagine the following method:
public void doSomethingWithAMonth(int monthNum);
Not only is this unclear (does the month index start at 1 or at 0?), However, you can drop the invalid data (13, negative number) in the lower right corner If you have an enumerated month with Jan, Feb, etc., the signature becomes:
public void doSomethignWithAMonth(Month month);
The code calling this method will be more readable and cannot provide invalid data
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码