0

Does enums support all data types?

23rd Sep 2016, 11:38 AM
Tabres Basha
Tabres Basha - avatar
2 Answers
+ 1
No Enums in compile time are broken down to a list of numbers. Say you have and enumeration of {"Cat", "Dog", "Mouse"} when you use that in your application it will be stored as 0, 1, 2 integer values. Instead of being stored as strings.
23rd Sep 2016, 6:23 PM
Catlin
Catlin - avatar
0
Of course... you can overwrite the constrctor to provide more meaningful info class Main { public static void main(String[] args) { Player player1 = new Player(Difficulty.EASY); Player player2 = new Player(Difficulty.MEDIUM); Player player3 = new Player(Difficulty.HARD); } } enum Difficulty { EASY(3000), MEDIUM(2000), HARD(1000); public final int bullets; private Difficulty(int bullets) { this.bullets = bullets; } } public class Player{ Player(Difficulty diff){ //your code goes here int bullets = diff.bullets; /* switch (diff) { case EASY: bullets = 3000; break; case MEDIUM: bullets = 2000; break; case HARD: bullets = 1000; break; } */ System.out.format("You have %d bullets", bullets); } }
21st Jan 2021, 12:35 AM
David OrdĂĄs
David OrdĂĄs - avatar