From: www on
Hi,

I am new to enum. Suppose I have the following enum

packcage A.B.C

enum CarModel
{
BMW, HONDA, FORD
}

In my program, I need to get the string "CarModel" from enum CarModel.
Is there a way to do it?

Note: I am not talking about getting string "BMW", "HONDA" etc. I know
CarModel.BMW.toString() will give me "BMW".

I also prefer to get "CarModel" string, not "A.B.C.CarModel".

Thank you very much.
From: Eric Sosman on
On 2/26/2010 8:33 AM, www wrote:
> Hi,
>
> I am new to enum. Suppose I have the following enum
>
> packcage A.B.C
>
> enum CarModel
> {
> BMW, HONDA, FORD
> }
>
> In my program, I need to get the string "CarModel" from enum CarModel.
> Is there a way to do it?
>
> Note: I am not talking about getting string "BMW", "HONDA" etc. I know
> CarModel.BMW.toString() will give me "BMW".
>
> I also prefer to get "CarModel" string, not "A.B.C.CarModel".

I'm not sure what you start with: From what, exactly, do you
want to derive the String? If you have a reference and you think
it points to an enum CarModel object, you could do something like

Object obj = ...; // might be FORD
String name = obj.getClass().getName();
name = name.substring(name.lastIndexOf('.') + 1);
if (name.contains("$"))
name = name.substring(name.lastIndexOf('$') + 1);

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: www on
Eric Sosman wrote:


>
> I'm not sure what you start with: From what, exactly, do you
> want to derive the String? If you have a reference and you think
> it points to an enum CarModel object, you could do something like
>
> Object obj = ...; // might be FORD
> String name = obj.getClass().getName();
> name = name.substring(name.lastIndexOf('.') + 1);
> if (name.contains("$"))
> name = name.substring(name.lastIndexOf('$') + 1);

Thank you. It is hard for me to explain why I need what I am asking for.

I feel your method is a little inconvenient for the client code. I think
I will just do this:

enum CarModel
{
BMW, HONDA, FORD;

static String getEnumName(){ return "CarModel";}

}
From: Mayeul on
www wrote:
> I will just do this:
>
> enum CarModel
> {
> BMW, HONDA, FORD;
>
> static String getEnumName(){ return "CarModel";}
>
> }

If this is enough to do what you want, then the getEnumName() method is
unnecessary.

You may just call CarModel.class.getName()

I was about to say you could just use the String litteral "CarModel",
but that would actually be weak to typos, while CarModel.class.getName()
is not.

--
Mayeul
From: Eric Sosman on
On 2/26/2010 9:06 AM, www wrote:
> Eric Sosman wrote:
>
>
>>
>> I'm not sure what you start with: From what, exactly, do you
>> want to derive the String? If you have a reference and you think
>> it points to an enum CarModel object, you could do something like
>>
>> Object obj = ...; // might be FORD
>> String name = obj.getClass().getName();
>> name = name.substring(name.lastIndexOf('.') + 1);
>> if (name.contains("$"))
>> name = name.substring(name.lastIndexOf('$') + 1);
>
> Thank you. It is hard for me to explain why I need what I am asking for.
>
> I feel your method is a little inconvenient for the client code. I think
> I will just do this:
>
> enum CarModel
> {
> BMW, HONDA, FORD;
>
> static String getEnumName(){ return "CarModel";}
>
> }

Okay, but the caller must write `CarModel.getEnumName()'
to call the method. That is, the caller already *knows* the
identifier `CarModel', and might have just said `"CarModel"'.
Oh, well -- this way you get an opportunity to internationalize,
so `CarModel.getEnumName()' might return "Automodell" in a
German-speaking locale.

--
Eric Sosman
esosman(a)ieee-dot-org.invalid