[663] in java-interest

home help back first fref pref prev next nref lref last post

Class.newInstance() question

daemon@ATHENA.MIT.EDU (Anselm.BairdSmith)
Wed Jul 12 15:57:55 1995

Date: Wed, 12 Jul 1995 18:54:22 +0200
From: "Anselm.BairdSmith" <Anselm.Baird_Smith@inria.fr>
To: java-interest@java.sun.com


Hi,

I am using a class loader to load classes into my (java)
application. At the time I instantiate the loaded classes, I would
like to provide the instatiation method with some
parameters. As the newInstance() method of Class doesn't take any
parameters, I thought I would try to define a "Make" static method on
my loadable classes:

public class Loadable {
    static Loadable Make (String param) {
        return new Loadable (param) ;
    }
    Loadable (String param) {
        ...
    }
}

Than, the module that loads classes does something like:

class = Class.forName ("Loadable") ;	// or whatever
return class.Make ("paramater") ;

However, the compiler reports an error here, because it has no means to
know that the loaded class will respond to Make. Is there any way to
workaround this (such as casting class to the class of Loadable
objects), without turning into C-mode (as does javai.c for the main
method of the class it loads) ?

The only solution I can see is to turn to instances: having a clone
method:

public class Loadable {
    Loadable clone (String param) { return new Loadable (param); }
}

and than:

class = Class.forName ("Loadable") ; 
return ((Loadable) class.newInstance()).clone (param);

But I don't like it very much...

Anselm.

Anselm.BairdSmith@inria.fr - http://www.inria.fr/koala/abaird.html


-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post