Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (5.3k points)

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 to such solution.

I have never used enums and I have been programing in Java for more than couple a years now. And apparently they changed a lot. Now they even do full blown support of OOP within themselves.

Now why and what for should I use enum in day to day programming?

1 Answer

0 votes
by (9.5k points)

You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.

If you use enums instead of integers (or String codes), you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.

Related questions

0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Feb 7, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...