Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2008

Tip: Looking for answers? Try searching our database.

Enum String

Thread view: 
Jason Cavett - 08 Feb 2008 02:15 GMT
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
Lew - 08 Feb 2008 02:31 GMT
> Is it possible to get a String back from an enum value?  Here is a
> sample of my enum:
[quoted text clipped - 19 lines]
> 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.

Signature

Lew

Jason Cavett - 08 Feb 2008 02:39 GMT
> > Is it possible to get a String back from an enum value?  Here is a
> > sample of my enum:
[quoted text clipped - 27 lines]
>
> - 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?
Mark Space - 08 Feb 2008 02:52 GMT
> 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?
Stefan Ram - 08 Feb 2008 02:40 GMT
>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
Roedy Green - 08 Feb 2008 07:17 GMT
On Thu, 7 Feb 2008 18:15:33 -0800 (PST), Jason Cavett
<jason.cavett@gmail.com> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.