#Enums
1 messages · Page 1 of 1 (latest)
<@&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>.
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.
@south quiver Respond to the message here :)
Okay so, using your example
Why can’t you just set some global variable within an existing class
Could you provide a code example that demonstrates what you mean specifically?
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
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?
Well you could create some static variable inside that class that was int UP = 1;?
And do setDirection(UP)?
No
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
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?
It's about the typing
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
Oh this makes a lot of sense now
Right I see
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
Are the constants within the enums called variables or what’re they called?
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
Interesting. So I’m assuming that because you’re calling them constants you can’t change them at runtime?
The values
constants
or enum constants if you prefer
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
But you can?
You can't change them, but you can change their fields if you add fields
But you shouldn't
Why shouldn’t you if Java allows you to? Just curious
Is it just bad practice?
Because of java type system being limited
there is no way in java to achieve true immutability
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
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
Like an arraylist?
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?