Re: Class<T> question
Attila Szegedi <[email protected]>
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <[email protected]> |
On 2007.12.05., at 17:25, Gart, Mitchell wrote: > Thanks, it worked to use Class<?> instead of just Class. > > In the example you cite, couldn't you have declared > > Map<Object, String> map = new HashMap<Object, String>(); > > Instead of > > Map<? extends Object, String> map = new HashMap<Object, String>(); Actually, the first form would have been right -- I was typing from memory, sorry. I'm not really good with generics yet. I guess I should just refrain from giving generics-related advice for now. Attila. > > > > - Mitch > > > -----Original Message----- > From: Discussion of advanced Java topics. > [mailto:[email protected]] On Behalf Of Attila Szegedi > Sent: Wednesday, December 05, 2007 10:49 AM > To: [email protected] > Subject: Re: [ADVANCED-JAVA] Class<T> question > > In my recent wrestling with "genericizing" a codebase that was > originally pre-Java 5, I'd suggest you try replacing "Class" with > "Class<?>". > > Alternatively, disable compiler warning for generics. No, seriously. > Honestly, my feeling is that programming with generics causes more > noise in source code than it eliminates. Yeah, it eliminates casts, > but previously I would write: > > Map map = new HashMap(); > ... > String x = (String)map.get(blah); > > Now I write monstrosities such as: > > Map<? extends Object, String> map = new HashMap<Object, String>(); > ... > String x = map.get(blah); > > So, I need to write one cast less, and four type specifiers more. Net > loss in code conciseness. > I guess I'm just getting old and resistant to change. > > Attila. > > On 2007.12.05., at 14:12, Gart, Mitchell wrote: > >> I am trying to get rid of some compiler warnings in our code. We >> have a >> method: >> >> public static Logger getLogger(Class c) { >> return new Log4jLogger(c.getName()); >> } >> >> and the compiler produces this warning: >> >> Class is a raw type. References to generic type Class<T> should be >> parameterized >> >> It is because Class is a generic type but I don't quite understand >> how I >> would change the declaration of the method, and the calls to the >> method, >> to make it parameterized and get rid of the warning. Can somebody >> explain this a little bit? Thanks. >> >> - Mitch Gart > > =================================== > This list is hosted by DevelopMentor(r) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > > =================================== > This list is hosted by DevelopMentor® http://www.develop.com > > View archives and manage your subscription(s) at http://discuss.develop.com Attila. -- home: http://www.szegedi.org weblog: http://constc.blogspot.com =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com