I have an enum in Java I’d like to serialize, so that when I call it from anywhere in the code, I get the lowercase representation of the name. Let’s say I have the following enum: I’d like to get the following: [Note]: I want to use the enum constants in uppercase, and when requesting the value get the lowercase
Tag: enums
Get index of enum from string?
I have a string value, I also have an array of strings and an enum containing the range also. To get the index of the string in the array, from the value supplied I write this: How do I do this for an enum? Can I use ordinal? Or do i have to make my own method? The method might
Java enums – choice between fields, abstract methods, and class level map
I have written a Java enum where the values have various attributes. These attributes could be stored in any of the following ways: Using fields: Using abstract methods: Using class level map: How should I decide when to prefer which? Thanks! Answer (I am answering my own question so that I can share some things I learned while trying out
How to implement enum with generics?
I have a generic interface like this: This interface has limited instances, hence it would be best to implement them as enum values. The problem is those instances have different type of values, so I tried the following approach but it does not compile: Any idea about this? Answer You can’t. Java doesn’t allow generic types on enum constants. They
Printing the enum’s name
I’m using eclipse + Android SDK on Ubuntu. I would like to print the name of a sensor type device, there a a lot of them and I want to do it automatically. If I use a I print the (int) type, but I would like the name of the enum. How could I do that? Answer For enumerations, you
Java enum elements with spaces?
Im working on java, I have created an enum as follows: Above example gives errors while using space in the name of element (i.e. north America). Any suggestions how to resolve above issue? Answer You can’t put a space in the middle of an identifier. Doing so ends that identifier and the parser assumes whatever comes next is a valid
How to convert enum value to int?
I have a function which return a type int. However, I only have a value of the TAX enumeration. How can I cast the TAX enumeration value to an int? Answer You’d need to make the enum expose value somehow, e.g. (I’ve changed the names to be a bit more conventional and readable, btw.) This is assuming you want the
Finding Java Enum types from code values?
We use code values in our database, and Enums in Java. When querying the database, we need to take a code value and get an Enum instance. Is it overkill to have a HashMap to avoid iteration? What would you do? Is there an easier way? Answer That’s exactly the approach I’d take to solve that particular problem. I see
Why shouldn’t Java enum literals be able to have generic type parameters?
Java enums are great. So are generics. Of course we all know the limitations of the latter because of type erasure. But there is one thing I don’t understand, Why can’t I create an enum like this: This generic type parameter <T> in turn could then be useful in various places. Imagine a generic type parameter to a method: Or
Java’s enum… Where are they created?
Since enum in C# are on the stack, I was wondering where enum, in Java, where created. On the stack? On the heap? In some mysterious other place? Enumeration in C# are more primitive than those in Java, this might explain why they are created on the stack… Where are they? I can’t find them! Thanks Answer Enums in Java