Enum String

Discussion in 'Java' started by Jason Cavett, Feb 8, 2008.

  1. Jason Cavett

    Jason Cavett Guest

    Is it possible to get a String back from an enum value? Here is a
    sample of my enum:

    // XML tags
    public enum XML {
    DATA_TAG("data");

    private final String tag;


    private XML(String tag) {
    this.tag = tag;
    }

    @Override
    public String toString() {
    return tag;
    }
    }

    Now, I could like to be able to just call: XML.DATA_TAG and that
    returns a String. Is this possible? Something is telling me that
    I'll always have to call the toString() function as well, but I wanted
    to check first.

    Thanks
     
    Jason Cavett, Feb 8, 2008
    #1
    1. Advertising

  2. Jason Cavett

    Lew Guest

    Jason Cavett wrote:
    > Is it possible to get a String back from an enum value? Here is a
    > sample of my enum:
    >
    > // XML tags
    > public enum XML {
    > DATA_TAG("data");
    >
    > private final String tag;
    >
    >
    > private XML(String tag) {
    > this.tag = tag;
    > }
    >
    > @Override
    > public String toString() {
    > return tag;
    > }
    > }
    >
    > Now, I could like to be able to just call: XML.DATA_TAG and that
    > returns a String. Is this possible? Something is telling me that
    > I'll always have to call the toString() function as well, but I wanted
    > to check first.


    Given that DATA_TAG is not of type String itself, yes, you'll have to call
    toString() just like for any other object if you want a String representation.

    --
    Lew
     
    Lew, Feb 8, 2008
    #2
    1. Advertising

  3. Jason Cavett

    Jason Cavett Guest

    On Feb 7, 9:31 pm, Lew <> wrote:
    > Jason Cavett wrote:
    > > Is it possible to get a String back from an enum value?  Here is a
    > > sample of my enum:

    >
    > > // XML tags
    > > public enum XML {
    > >   DATA_TAG("data");

    >
    > >   private final String tag;

    >
    > >   private XML(String tag) {
    > >    this.tag = tag;
    > >   }

    >
    > >   @Override
    > >   public String toString() {
    > >    return tag;
    > >   }
    > > }

    >
    > > Now, I could like to be able to just call: XML.DATA_TAG and that
    > > returns a String.  Is this possible?  Something is telling me that
    > > I'll always have to call the toString() function as well, but I wanted
    > > to check first.

    >
    > Given that DATA_TAG is not of type String itself, yes, you'll have to call
    > toString() just like for any other object if you want a String representation.
    >
    > --
    > Lew- Hide quoted text -
    >
    > - Show quoted text -


    Alright. I sort of figured that I would. It kind of stinks, though,
    because wherever I was using my static Strings before, I now have to
    do:

    Data::XML::DATA_TAG.toString()
    instead of
    Data::DATA_TAG

    Seems kind of dumb to make the XML tags into an enum. Any opinion/
    argument one way or another?
     
    Jason Cavett, Feb 8, 2008
    #3
  4. Jason Cavett

    Stefan Ram Guest

    Jason Cavett <> writes:
    >Now, I could like to be able to just call: XML.DATA_TAG and that
    >returns a String. Is this possible?


    You can pass an object instead of the string value of that
    object, if the callee will invoke »toString()« himself.

    For example, the following programm prints the string value
    »alpha« of the object »new Text()« even though »toString()«
    is not explicitly invoked within the following source code.

    class Text
    { public java.lang.String toString(){ return "alpha"; }}

    public class Main
    { public static void main( final java.lang.String[] args )
    { java.lang.System.out.println( new Text() ); }}

    alpha
     
    Stefan Ram, Feb 8, 2008
    #4
  5. Jason Cavett

    Mark Space Guest

    Jason Cavett wrote:

    > Seems kind of dumb to make the XML tags into an enum. Any opinion/
    > argument one way or another?


    Java has several XML parsers available in the standard API. SAX, JDOM,
    XPath, plus probably a few more. Maybe look at one of those?
     
    Mark Space, Feb 8, 2008
    #5
  6. Jason Cavett

    Roedy Green Guest

    On Thu, 7 Feb 2008 18:15:33 -0800 (PST), Jason Cavett
    <> wrote, quoted or indirectly quoted someone
    who said :

    >Is it possible to get a String back from an enum value?


    yes. You can go either way. See valueOf and toString.

    see http://mindprod.com/jgloss/enum.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Feb 8, 2008
    #6
    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. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    548
  2. Eric Sosman
    Replies:
    3
    Views:
    781
    Mayeul
    Feb 26, 2010
  3. Wojtek
    Replies:
    1
    Views:
    502
  4. Lew
    Replies:
    0
    Views:
    538
  5. Roedy Green
    Replies:
    0
    Views:
    732
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page