Prev: launch the default application for a given file from Java?
Next: Prizes worth 104,000 INR at stake for RAD. Certificates to all participants.
From: Lew on 26 Feb 2010 22:40 www wrote: > Hi, > > I am new to enum. Suppose I have the following enum > > packcage A.B.C This line will not compile, due to the misspelling and lack of semicolon. Conventionally, packages are named with all lower-case letters. > 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? Of course. > 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". It's rather bizarre to use a class name; I've seen it done in production code many times where it causes fragility, inefficiency and bugs. Part of the problem is that use of the class name violates a kind of programming Gödelian incompleteness. It breaks type safety when used carelessly, as I've often seen it used, by passing class relationships around through strings rather than types. Some uses I've seen require particular naming relationships between types for them to work together, though actual object-oriented programming would have worked more easily and more flexibly. Regardless, you do it the usual way, with <http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getSimpleName()> -- Lew
From: Lew on 26 Feb 2010 22:44 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
From: Roedy Green on 26 Feb 2010 23:52
On Fri, 26 Feb 2010 08:33:20 -0500, www <www(a)nospam.com> wrote, quoted or indirectly quoted someone who said : > >In my program, I need to get the string "CarModel" from enum CarModel. >Is there a way to do it? see http://mindprod.com/jgloss/enum.html -- Roedy Green Canadian Mind Products http://mindprod.com The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at or repair. ~ Douglas Adams (born: 1952-03-11 died: 2001-05-11 at age: 49) |