Loading classes with primitive types in constructor

M

munki

I have some code like this, which works fine:

ClassLoader myClassLoader = TestConstructors.class.getClassLoader();
try {
Class myClass =
myClassLoader.loadClass("net.munki.util.classloader.Module");
String arg = "HELLO";
Class[] parameters = new Class[] { arg.getClass() };
Constructor c = myClass.getConstructor(parameters);
Object o = c.newInstance(new Object[] { arg });
net.munki.util.classloader.Module m =
(net.munki.util.classloader.Module)o;
m.start();
}
catch ...

It instantiates a class passing "HELLO" to one of the constructors, and
executes a method on the resulting object. So far so good. However, I also
have a constructor that takes an int as a parameter. Since I can only pass
an Object[] to the newInstance() of a Constructor, how am I going to pass my
int in order to instantiate an object?

Thx

munki
 
C

Chris Smith

munki said:
It instantiates a class passing "HELLO" to one of the constructors, and
executes a method on the resulting object. So far so good. However, I also
have a constructor that takes an int as a parameter. Since I can only pass
an Object[] to the newInstance() of a Constructor, how am I going to pass my
int in order to instantiate an object?

You use wrapper classes for primitive types. These are classes in
java.lang called Byte, Short, Integer, Long, Character, Boolean, Float,
and Double. When you build the Class[], though, be sure to use the
primitive class literal. For example:

Constructor ctor = cls.getConstructor(new Class[] { int.class });
Object obj = ctor.newInstance(new Object[] { new Integer(7) });

In general, it's a bad idea to build the type array for getConstructor
by using getClass() on the arguments. It breaks down not just for
primitives, but also whenever you want to pass an object where the
constructor is declared with a supertype for the parameter. In essence,
it only works if you don't take advantage of inheritance. You should
*know* the type, and specify it literally as "String.class", not
"arg.getClass()".

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Michael Borgwardt

munki said:
It instantiates a class passing "HELLO" to one of the constructors, and
executes a method on the resulting object. So far so good. However, I also
have a constructor that takes an int as a parameter. Since I can only pass
an Object[] to the newInstance() of a Constructor, how am I going to pass my
int in order to instantiate an object?

The API doc of the newInstance() method tells you that, who'd have thunk it?
 
M

munki

The API doc of the newInstance() method tells you that, who'd have thunk
it?Yeah apologies, I missed it. I'll go and beat myself severely with the JLS.

munki
 
M

munki

You use wrapper classes for primitive types. These are classes in
java.lang called Byte, Short, Integer, Long, Character, Boolean, Float,

Thanks Chris, just what I need. And thanks too for the advice on usage.

munki
 
M

Michael Borgwardt

munki said:
it?

Yeah apologies, I missed it. I'll go and beat myself severely with the JLS.

The API, not the JLS. You rarely need to consult the JLS for your daily work
as a programmer, but the platform API docs are absolutely essential.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top