#Enums

1 messages · Page 1 of 1 (latest)

proud cloudBOT
#

@south quiver has a question:

elimswastaken

Can someone explain to me what enums are

#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Can someone explain to me what enums are

Enums in Java are a special data type that allow you to define a set of named constants. They provide a way to create a group of related constants that can be used across your codebase.

Enums are declared using the "enum" keyword and can contain a list of predefined values. These values are typically written in uppercase letters, separated by commas.

For example, you could create an enum called "DaysOfWeek" with the constants MONDAY, TUESDAY, WEDNESDAY, etc. This would allow you to refer to these days in your code using the enum instead of hardcoding string values.

Enums are often used in switch statements or as parameters for methods to ensure type safety and avoid magic numbers or strings in your code. They also make your code more readable and maintainable by providing meaningful names for constant values.

Additionally, enums can have methods and fields just like regular classes, allowing you to add behavior or additional data to each constant value.

Overall, enums are a powerful feature in Java that help you organize and manage constant values in your codebase effectively.

lament glacier
#

@south quiver Respond to the message here :)

south quiver
#

Okay so, using your example

#

Why can’t you just set some global variable within an existing class

lament glacier
#

Could you provide a code example that demonstrates what you mean specifically?

south quiver
#

I’m on mobile but like, as I understand it, enums are for setting some constants, right?

#

That can be used throughout the project

#

What’s the difference between using an enum for that and just creating some class with static variables that can also be used as constants

lament glacier
#

I’ll try my best to ELI5 this. So using the example I sent, how would you describe the four directions and pass one specific one into a function? Aka, how would you write a setDirection() function?

#

We could try assuming that 1 is North, 2 is South, 3 is East etc

#

And if we wanted to set the direction to east, we could say: setDirection(3)

#

(Also on mobile btw so a bit slow)

#

But is that setDirection(3) readable the way it is?

#

How would you know that 3 is East if I never told you that 3 equals east?

south quiver
#

Well you could create some static variable inside that class that was int UP = 1;?

#

And do setDirection(UP)?

lament glacier
#

You could do that but it wouldn’t scale well

#

Let’s say in the future you wanted to add different types of candies in that same code or something

#

It’s better to organize it with an enum

south quiver
#

Right, but I guess I’m wondering is, what’s the difference between writing a class that was DaysOfTheWeek with static variables for int MONDAY = 1; and so on and creating an enum for this

#

Do enums have some other functionality I’m not aware of?

stark oriole
#
void setDirection(int direction)

vs

void setDirection(Direction direction)
#

in the first one

#

what are you supposed to pass ?

#

how do you know that the direction is not just any int but a specific constant ?

#

and what if you do the mistake ?

#

then you just passed a random int by mistake

#

you would need to do a check in setDirection just to be sure

#

and even if you do that, you have no compile time check, the compiler won't tell you anything if you pass any int, while with an enum, it will tell you that you have to pass that enuml constant

south quiver
stark oriole
#

so two things :

#
  • Readability
  • Compile time checks
#

Ah and also

#

Java enums specifically are more than that

#

they are objects

#

so there is a lot of stuff you can do

#

Direction.values() would return an array of every direction

south quiver
#

Are the constants within the enums called variables or what’re they called?

stark oriole
#

Direction d = Direction.LEFT;
println(d) // this would print LEFT

#

in fact

#

calling toString() or name() on an enum constant would return the name of the constant

#

and you can still get the index of the constant with ordinal()

#

And Direction.valueOf("LEFT") would find and return Direction.LEFT

#

etc

south quiver
#

Interesting. So I’m assuming that because you’re calling them constants you can’t change them at runtime?

#

The values

stark oriole
#

but they are constants

#

and you shouldn't try to change them

#

ah and also

#

in java, since those constants are objects, they can have fields...

#

@south quiver

south quiver
stark oriole
south quiver
#

Is it just bad practice?

stark oriole
#

there is no way in java to achieve true immutability

south quiver
# stark oriole Because of java type system being limited

This is really interesting and I wanna understand it properly, do you know where I could learn about this in more detail or is it just through experience? I’m asking because otherwise I could be asking you questions all day haha

stark oriole
#

It's fine

#

Enums are regular classes

#

and nothing prevent you in a regular class to have non final fields

#

and even if it was the case (which is the case of records), nothing prevents you to have a mutable object in the final field

south quiver
#

Cuz you can modify the contents of an array list right? even if it’s final, you just can’t reference a different array list object?