• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class and generics

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

My goal is to create object by it's name using non-default constructor.

Let's consider the following simple "hello world" program to do this:



With Java 1.6 it works fine, but gives compile time warning:

Note: Hello.java uses unchecked or unsafe operation
Note: Recompile with -Xlint:unchecked for details.

I know the problem is with generics, specifically here: clazz.getDeclaredConstructor(new Class[] {java.lang.String.class}); But how should i change this line to avoid warning?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either use the compiler option or @SuppressWarnings("unchecked"). If you add the cast I think you'd get an unchecked cast warning, which means you'd have to do the same thing anyway. You could also make the clazz declaration generic and put the suppression there, and generify the Constructor init.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do something like this, which compiles cleanly, with no annotations. It's really the Class.forName() which prevents you from doing this any other way.

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh; I like that better. (Although is the arg list <?> necessary?)
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote: (Although is the arg list <?> necessary?)



No, I guess not -- I got carried away
 
Andrey Kozhanov
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:You can do something like this, which compiles cleanly, with no annotations. It's really the Class.forName() which prevents you from doing this any other way.



Yup, it worked. Thanks a lot!
 
reply
    Bookmark Topic Watch Topic
  • New Topic