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 / September 2006

Tip: Looking for answers? Try searching our database.

Class.forName usage

Thread view: 
Moiristo - 25 Sep 2006 17:31 GMT
A short question about forName:

I have an interface called 'GeometryPainter' and a subclass that
implements it, which is called 'DefaultGeometryPainter'. I'm
instantiating the DefaultGeometryPainter as following (Java5):

 Class painter  = Class.forName(lpainterClass);
 GeometryPainter gm = GeometryPainter.class.cast(painter);

It finds the class, because I don't get a ClassNotFoundException, but I
cannot cast it to GeometryPainter (it throws a ClassCastException). Can
someone tell me why? What should I do?
jeffcoat - 25 Sep 2006 18:35 GMT
> A short question about forName:
>
[quoted text clipped - 8 lines]
> I cannot cast it to GeometryPainter (it throws a
> ClassCastException). Can someone tell me why? What should I do?

You don't want a cast, you want to call Class.newInstance().

Something like ...

try {
   Class painter = Class.forName("DefaultGeometryPainter");
   GeometryPainter gm = (GeometryPainter) painter.newInstance();
} catch ...

If you want something other than the default constructor,
look into Class.getConstructor().

Signature

Mark Jeffcoat

Moiristo - 26 Sep 2006 00:12 GMT
> You don't want a cast, you want to call Class.newInstance().
>
[quoted text clipped - 7 lines]
> If you want something other than the default constructor,
> look into Class.getConstructor().

Thank you both, it's working now.
jagonzal@gmail.com - 25 Sep 2006 22:35 GMT
> A short question about forName:
>
[quoted text clipped - 8 lines]
> cannot cast it to GeometryPainter (it throws a ClassCastException). Can
> someone tell me why? What should I do?

This code: Class painter  = Class.forName(lpainterClass);

Gives you the object that represents the _class_ of that object, not an
instance of the class.

For that you need to call newInstance() on the Class object (if you
have a no-argument constructor), or get the constructors, and call one
of them according to the parameters it takes.

Check the Class javadocs.


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.