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: www on 26 Feb 2010 10:28 Mayeul wrote: > > 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 I just tested. CarModel.class.getName() gives me "A.B.C.CarModel", not "CarModel". Then I have to extract the string out. Yes, you are right that your method avoids typos. I am trying this: enum CarModel { BMW, HONDA, FORD; static String getEnumName() //{ return "CarModel";} //weak to typs { return ??? //trying to get enum type CarModel and convert to string "CarModel", to avoid using the String litteral "CarModel". But I cannot figure out how to do it. } }
From: Mayeul on 26 Feb 2010 10:36 www wrote: > I just tested. CarModel.class.getName() gives me "A.B.C.CarModel", not > "CarModel". Then I have to extract the string out. My mistake, I meant CarModel.class.getSimpleName() Which is only available since Java 1.5, I had forgotten. Still, a better idea if available. -- Mayeul
From: www on 26 Feb 2010 10:38 Mayeul wrote: > www wrote: >> I just tested. CarModel.class.getName() gives me "A.B.C.CarModel", not >> "CarModel". Then I have to extract the string out. > > My mistake, I meant CarModel.class.getSimpleName() > Which is only available since Java 1.5, I had forgotten. Still, a better > idea if available. > > -- > Mayeul Thank you very much. It works well for me. CarModel.class.getSimpleName() gives me exactly what I want, the String "CarModel".
From: Wojtek on 26 Feb 2010 12:40 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 :-)
From: www on 26 Feb 2010 13:17
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(); > 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? 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; } } |