My question is "is it possible to have an int identifier in an enum?" I'm developping a java program and I need to have identifiers that contains numbers and letters but eclips doesn't accept it.
for exapmle public enum Tag{ 5F25, 4F, . . . }
Do anyone know if there is any way to solve this problem! Thank you
Enum instances must obey the same java language naming restrictions as other identifiers - they can't start with a number.
If you absolutely must have hex in the name, prefix them all with a letter/word, for example the enum class itself:
public enum Tag {
Tag5F25,
Tag4F,
...
}
or maybe with an underscore and all-caps:
public enum Tag {
TAG_5F25,
TAG_4F,
...
}
etc
You're trying to bend the laws and conventions of programming. I suggest you take a different approach.
Enums can have constructors. They are usually private, because "instances" of the enum are created inside it. You can provide any info you want (e.g. id, name, keyword etc.).
In this example, I've implemented the enum with just one parameter in the constructor, which is the unique ID you're needing.
public enum Tag
{
TAG_1(1),
TAG_2(2),
TAG_3(3);
private final int id;
private Tag(final int id)
{
this.id = id;
}
public int id() // Objective C style. Can be getId()
{
return id;
}
/**
* Bonus method: get a Tag for a specific ID.
*
* e.g. Tag tagWithId2 = Tag.getTagForId(2);
*/
public static Tag getTagForId(final int id)
{
for (final Tag tag : values())
if (tag.id == id)
return tag;
return null;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With