Re: an enum question - how to convert enum type to a string?

Discussion in 'Java' started by Wojtek, Feb 26, 2010.

  1. Wojtek

    Wojtek Guest

    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?
    >
    > I also prefer to get "CarModel" string, not "A.B.C.CarModel".


    CarModel.BMW.getDeclaringClass().getSimpleName();

    Though I am also confused. To reach BMW you must already know that it
    is a part of CarModel

    BTW, you do know that you can give enums parameters?

    enum CarModel
    {
    BMW("This is a BMW"),
    HONDA("This is a Honda"),
    FORD("This is a Ford");

    String title;

    CarModel(String aTitle)
    {
    title=aTitle;
    }

    public getTitle()
    {
    return title;
    }
    }

    CarModel.BMW.getTitle();

    --
    Wojtek :)
    Wojtek, Feb 26, 2010
    #1
    1. Advertising

  2. Wojtek

    Lew Guest

    Wojtek wrote:
    >> BTW, you do know that you can give enums parameters?
    >>
    >> enum CarModel
    >> {
    >> BMW("This is a BMW"),
    >> HONDA("This is a Honda"),
    >> FORD("This is a Ford");
    >>
    >> String title;
    >>
    >> CarModel(String aTitle)
    >> {
    >> title=aTitle;
    >> }
    >>
    >> public getTitle()
    >> {
    >> return title;
    >> }
    >> }
    >>
    >> CarModel.BMW.getTitle();


    www wrote:
    > Thanks a lot. The tip really helps. I didn't understand this when I was
    > reading some enum materials found online. They are too confusing. You
    > cleared my mind. BTW, I think the constructor is private, right?


    Not in Wojtek's example.

    > For me, this is the first real case of private constructor.
    >
    > enum CarModel
    > {
    > BMW("This is a BMW"),
    > HONDA("This is a Honda"),
    > FORD("This is a Ford");
    >
    > private String title;
    >
    > private CarModel(String aTitle)
    > {
    > title=aTitle;
    > }
    >
    > public getTitle()
    > {
    > return title;
    > }
    > }


    I would add a static 'fromTitle(String title)' method that works similarly to
    'valueOf()' but matches enum values to input title strings.

    --
    Lew
    Lew, Feb 27, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. guoqi zheng

    convert type string to type guid

    guoqi zheng, Jul 2, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    13,370
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Jul 2, 2005
  2. Replies:
    6
    Views:
    953
    Greg Comeau
    Oct 20, 2005
  3. Eric Sosman
    Replies:
    3
    Views:
    778
    Mayeul
    Feb 26, 2010
  4. Lew
    Replies:
    0
    Views:
    535
  5. Roedy Green
    Replies:
    0
    Views:
    729
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page