Reputation: 5387
I am trying to define an enum type with Integer values. I am trying to do this because I use this particular value in multiple places in my code and it can only be this. The code below does not work. I guess I am not supposed to use enum like this. Instead should I use a class and put the possible values in an array?
public enum Pitch {
"60" , "62", "64", "65", "67", "69", "71", "72", "74";
}
Upvotes: 1
Views: 7795
Reputation: 3956
You have to specify named LITERALS
for enums.
What you can do is
public enum Pitch
{
SOMENAME(60),
SOMEOTHERNAME(62),
// ...
SOMELASTNAME(74);
public final int value;
public Pitch(int val)
{
this.value = val;
}
}
Then you can access your specific values by name and you can write
Pitch pitch = ...;
int pitchInt = pitch.value;
to work with the values.
Upvotes: 1
Reputation: 660
If you like to access the values over an index, you should use an array like
String[] pitch = new String[] {
"60" , "62", "64", "65", "67", "69", "71", "72", "74"
};
An enum is used to dont keep values in mind and give them names instead:
public enum Weekday {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
So you access the values over a name instead of an index.
Upvotes: 0
Reputation: 16351
I'm not sure enum is the best choice here, but you could try something like:
public enum Pitch {
p60(60), p62(62), ...;
private final int pitch;
Pitch(int pitch) {
this.pitch = pitch;
}
public int getValue() {
return pitch;
}
}
Upvotes: 2