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 / October 2005

Tip: Looking for answers? Try searching our database.

Generic method question

Thread view: 
tiboweb@btcweb.com - 19 Oct 2005 12:37 GMT
Hi,

I am using a method:
   Object lookup(Class c) {...}

The lookup method return an instance of the class c.

Is there is any way using java generic methods to insure compile-time
type checking? Something like:
   T lookup(Class <T?????>)

Thanks.
Ross Bamford - 19 Oct 2005 13:11 GMT
> Hi,
>
[quoted text clipped - 8 lines]
>
> Thanks.

I assume you mean something like:

  public <T> T lookup(Class<? extends T> clazz)
  throws IllegalAccessException, InstantiationException {
    return clazz.newInstance();
  }

P.s. compile time type safety is pretty much _all_ generics are good for ;)

Signature

Ross Bamford - rosco@roscopeco.remove.co.uk

tiboweb@btcweb.com - 19 Oct 2005 15:09 GMT
OK, that's working. Thanks all.
Ingo R. Homann - 19 Oct 2005 13:12 GMT
Hi,

> Hi,
>
[quoted text clipped - 6 lines]
> type checking? Something like:
>     T lookup(Class <T?????>)

Yes, exactly:

T lookup(Class<T> c) {...}

Ciao,
Ingo
Roedy Green - 19 Oct 2005 14:20 GMT
>Is there is any way using java generic methods to insure compile-time
>type checking? Something like:
>    T lookup(Class <T?????>)

see http://mindprod.com/jgloss/generics.html#FACTORIES
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



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



©2009 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.