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 / November 2007

Tip: Looking for answers? Try searching our database.

Howe to correctly parameterise a Class

Thread view: 
Adam Lipscombe - 06 Nov 2007 10:40 GMT
Folks

How do I correctly parameterise a Class?

I have code like this:

String classPath = "com.blah.bah.MyClass"
Class clazz = Class.forName(classPath);
Facade facade = (Facade) clazz.newInstance();

Facade is an interface and the class named by classPath will always implement this i/f.

Do I simply do: Class<Facade> clazz = Class.forName(classPath);

If not, what?

TIA - Adam
Thomas Fritsch - 06 Nov 2007 11:44 GMT
> String classPath = "com.blah.bah.MyClass"
> Class clazz = Class.forName(classPath);
[quoted text clipped - 4 lines]
>
> Do I simply do: Class<Facade> clazz = Class.forName(classPath);
Not quite.
Class.forName() is declared to return Class<?>, but you want Class<Facade>.

> If not, what?
You'll still need a cast:
  Class<Facade> clazz = (Class<Facade>) Class.forName(classPath);
  Facade facade = clazz.newInstance();

Signature

Thomas

Piotr Kobzda - 06 Nov 2007 12:58 GMT
[...]

>> If not, what?
> You'll still need a cast:
>    Class<Facade> clazz = (Class<Facade>) Class.forName(classPath);
>    Facade facade = clazz.newInstance();

I prefer to use a bit type-safer version of the above:

    Class<? extends Facade> clazz
            = Class.forName(classPath).asSubclass(Facade.class);
    Facade facade = clazz.newInstance();

piotr
Thomas Fritsch - 06 Nov 2007 13:26 GMT
>> You'll still need a cast:
>>    Class<Facade> clazz = (Class<Facade>) Class.forName(classPath);
[quoted text clipped - 5 lines]
>              = Class.forName(classPath).asSubclass(Facade.class);
>      Facade facade = clazz.newInstance();
Granted! I prefer yours, too.

Signature

Thomas

Adam Lipscombe - 06 Nov 2007 14:38 GMT
Groovy! Many thanks....

Adam
Daniel Pitts - 06 Nov 2007 16:11 GMT
> [...]
>
[quoted text clipped - 10 lines]
>
> piotr
Not to mention that this is more correct.
The Class instance itself is the MyClass, not Facade, so Class<Facade>
is just plain wrong, where as Class<? extends Facade> is accurate.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>



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



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