Having a little problem with reflection

F

fishfry

Class c = Class.forName(classname);
classname c = new classname();

Doesn't seem to compile. My understanding is that the first line creates
c as a class whose name is classname. But I can't seem to use line 2 to
instantiate an object of that class. Do I have to use getConstructor()?
I can't seem to find any specific examples of this in the Sun tutorials,
perhaps someone can straighten me out. Thanks in advance.
 
T

Tony Morris

fishfry said:
Class c = Class.forName(classname);
classname c = new classname();

Doesn't seem to compile. My understanding is that the first line creates
c as a class whose name is classname.
The class loader simply loads the class called classname.
A reference to the class is returned and assigned to 'c'.
But I can't seem to use line 2 to
instantiate an object of that class.
Why not ? There is nothing special you are doing (that we can see) that
prevents such a thing.
What is the compile-time error ?
Do I have to use getConstructor()? No.

I can't seem to find any specific examples of this in the Sun tutorials,
perhaps someone can straighten me out. Thanks in advance.

You are performing a dynamic class load, followed by a static instantiation.
Very odd. Do you mean to instantiate the Class object ?
Use the newInstance() method.
If you haven't got a default constructor (speculating that this is the
source of your compile-time error), then this will cause an exception.
More information required (the compile-time error).

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
T

Thomas Weidenfeller

fishfry said:
Class c = Class.forName(classname);
classname c = new classname();

Doesn't seem to compile.

Provide REAL code and the REAL error message.

/Thomas
 
F

fishfry

Tony Morris said:
The class loader simply loads the class called classname.
A reference to the class is returned and assigned to 'c'.

Why not ? There is nothing special you are doing (that we can see) that
prevents such a thing.
What is the compile-time error ?


You are performing a dynamic class load, followed by a static instantiation.
Very odd. Do you mean to instantiate the Class object ?
Use the newInstance() method.
If you haven't got a default constructor (speculating that this is the
source of your compile-time error), then this will cause an exception.
More information required (the compile-time error).

Ok I have a dynamic instantiation going like so:

Class c = Class.forName(classname);
Object o = c.newInstance();

That is working ok. However now I need to pass an argument to the
constructor:

Object o = c.newInstance("foo")

The compiler complains "newInstance() in java.lang.Class cannot be
applied to (java.lang.String)"
 
?

=?ISO-8859-1?Q?Fabian_B=FCttner?=

The following code works:

import java.lang.reflect.*;
class MyClass {
public MyClass(String arg) {
System.out.println("constructor called with " + arg);
}
}

class MyTestClass {
public static void main(String[] args) throws Exception {
Class c = Class.forName("MyClass");
Constructor con = c.getConstructor( new Class[]{String.class} );
con.newInstance( new Object[]{"Hello World"} );
}
}

Regards,
Fabian
 
C

Collin VanDyck

Ok I have a dynamic instantiation going like so:

Class c = Class.forName(classname);
Object o = c.newInstance();

That is working ok. However now I need to pass an argument to the
constructor:

Object o = c.newInstance("foo")

The compiler complains "newInstance() in java.lang.Class cannot be
applied to (java.lang.String)"

Try using the Class.getDeclaredConstructor(Class[] parameterTypes) to get
the Constructor.
ex:
Class[] parameterTypes = new Class[1];
parameterTypes[0] = "".getClass();
Constructor constructorWithStringParam =
c.getDeclaredConstructor(parameterTypes);

Once you have the Constructor, use

Constructor.newInstance(Object[] initargs)
ex:
Object[] initargs = new Object[1];
initargs[0] = "first param";
Object o = constructorWithStringParam.newInstance(initargs);

To call the constructor with the String argument.

-CV
 
F

fishfry

Fabian Buttner said:
The following code works:

import java.lang.reflect.*;
class MyClass {
public MyClass(String arg) {
System.out.println("constructor called with " + arg);
}
}

class MyTestClass {
public static void main(String[] args) throws Exception {
Class c = Class.forName("MyClass");
Constructor con = c.getConstructor( new Class[]{String.class} );
con.newInstance( new Object[]{"Hello World"} );
}
}


That's what I'm doing now, except that my arg is actually a JFrame and
not a string. So I'm doing

Constructor ct
= c.getConstructor(new Class[]{JFrame.class});

This compiles ok, but the above line throws a NoSuchMethodException at
runtime. The class being forName'd definitely has exactly one
constructor, which DOES take a JFrame. I'm wondering what could be the
problem.
 
F

fishfry

fishfry said:
Fabian Buttner said:
The following code works:

import java.lang.reflect.*;
class MyClass {
public MyClass(String arg) {
System.out.println("constructor called with " + arg);
}
}

class MyTestClass {
public static void main(String[] args) throws Exception {
Class c = Class.forName("MyClass");
Constructor con = c.getConstructor( new Class[]{String.class} );
con.newInstance( new Object[]{"Hello World"} );
}
}


That's what I'm doing now, except that my arg is actually a JFrame and
not a string. So I'm doing

Constructor ct
= c.getConstructor(new Class[]{JFrame.class});

This compiles ok, but the above line throws a NoSuchMethodException at
runtime. The class being forName'd definitely has exactly one
constructor, which DOES take a JFrame. I'm wondering what could be the
problem.

One more clue. I tried it with MyClass exactly as above and it worked
fine. However what I'm doing is making a dynamic call to a class defined
in a separate file and compiled separately into a class file in the same
directory. That seems to be the difference. Any suggestions gratefully
appreciated.
 

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,011
Latest member
AjaUqq1950

Latest Threads

Top