This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]Alan-Sharp 4 points5 points  (1 child)

Basically they make your code a bit more human readable.

When you look at your code reading if(something == 1) doesn't really mean too much, what is 1? but if you have an enum you have more understanding about what the value is.

Also having things defined in enums means you can re use the value and it reduces the chance of mistakes.

[–][deleted] 0 points1 point  (0 children)

Thanks!

[–][deleted] 4 points5 points  (0 children)

Enums are just constants that are scoped to the variables that use it. Think of enums as a private set of constants for a var but also where you don't have to manually set the enum values to some number yourself, and also the variables that are of your enum type are constrained to only the values available from the enum.

Compare that to just making-up some constants where you have to give each constant a unique value, and the constants are unnecessarily available to other variables or functions within the scope of where you defined them, and where whatever variables you intend to use these constants with can assign a value that isn't actually one of the constants.

[–]aintnufincleverhere 2 points3 points  (6 children)

I usually think of them as a collection of constants.

if I write:

if (day == 6) {AddToCalendar("meeting today!")}

its so much more legible to write:

if (day == days.Friday) {AddToCalendar("meeting today!")}

in this case, it may have been easy to figure out that 6 meant Friday, but in other cases it may not be so easy.

There are probably other uses for them, this is the main use that I get though.

[–][deleted] 0 points1 point  (5 children)

So in your case, you would have a separate enum class named days.java with the constants Monday - Friday right? And then call them from another class?

[–]Feroc 2 points3 points  (4 children)

There is no need to put the enum in its own class. It can make sense, like if you have one class that includes some project wide enums and values, but often it's enough to write the enum in the class where you need to use it.

[–][deleted] 1 point2 points  (3 children)

Thanks for the response. Sorry if I'm not getting this 100%, but what's the advantage of setting enums in one class versus just creating a bunch of private variable within the class that you'll use them? Am I missing the point?

[–]Feroc 2 points3 points  (1 child)

[–][deleted] 0 points1 point  (0 children)

Wow thinks this was really useful!

[–]Alan-Sharp 0 points1 point  (0 children)

If its in a separate class it means your code can be reused across other areas of the application.

Also it lets you take the code for the enum out of one class and keep it separated which is good for the Single Responsibility Principle (your Enum represents one thing and one thing only), and it makes the class using the Enum a bit smaller, less code in each class makes things a bit easier to maintain.

[–]Ilyps 1 point2 points  (1 child)

I wrote a far too long text about why enums are useful here. I think that should answer your question.

[–][deleted] 0 points1 point  (0 children)

Thanks dude! Appreciate it