Class.forName usage

M

Moiristo

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?
 
J

jeffcoat

Moiristo said:
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?


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().
 
J

jagonzal

Moiristo said:
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?

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.
 
M

Moiristo

jeffcoat said:
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().

Thank you both, it's working now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top