[3618] in java-interest
re:Dynamic Object Instantiation ....
daemon@ATHENA.MIT.EDU (andrew (a.) francis)
Sat Nov 18 17:34:25 1995
Date: Fri, 17 Nov 1995 10:01:00 -0500
From: "andrew (a.) francis" <andrewfr@bnr.ca>
To: neudeck.arthur@ch.swissbank.com
Cc: java-interest@java.sun.com
In message "Dynamic Object Instantiation ....", you write:
> I load a new class by a given name from a directory.
> Afterwards I instantiate a new Object that way:
> Object newObject = Class.forName("...")
>
> Everything works fine until the moment I try to call a method of this new
> object. I do have the problem, that the Class object does not provide the
> called method, e.g.:
> newObject.getOID();
>
> How can I instantiate the object with the right cast???
What you could do is have your object implement an interface, say
Dummy, that declares the methods that your class implements. For
example:
public interface Dummy
{
public void Print();
}
and
public class MyClass implements Dummy
{
.
.
.
Once you have loaded your class (by whatever means), instantiate
it,
Object x = myClass.newInstance();
And then cast using the interface, to activate methods, i.e
( (Dummy) x).Print();
This was the approach recommended to be used to handle classes that
come from a custom classloader.
Cheers,
Andrew
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com