I'm trying to figure out reflection, and I've run into a problem. In the
code below the JFrame and String get instantiated fine, but MyClass gives
an InstantiationException.
What am I overlooking here?
import java.util.ArrayList;
public class ReflectionTest
{
public static void main(String[] args) throws Exception
{
ArrayList<String> typeNames = new ArrayList<String>();
typeNames.add(String.class.getName());
typeNames.add(javax.swing.JFrame.class.getName());
// typeNames.add(MyClass.class.getName());
ArrayList<Object> data = new ArrayList<Object>();
for(String name : typeNames)
data.add(Class.forName(name).newInstance());
System.out.println(data.toString());
}
private class MyClass
{
public MyClass()
{
super();
}
public String toString()
{
return "MyClass";
}
}
}
Chris Uppal - 06 Nov 2005 13:53 GMT
> I'm trying to figure out reflection, and I've run into a problem. In the
> code below the JFrame and String get instantiated fine, but MyClass gives
> an InstantiationException.
>
> What am I overlooking here?
> public MyClass()
> {
> super();
> }
Despite appearances, MyClass (or rather ReflectionTest$MyClass, to give it its
proper name), does not have a no-args constructor. Since it is an inner class,
it contains a hidden reference to its outer object. That reference is passed
in as a hidden parameter to every constructor (the parameter is automatically
added by the compiler). So there is no no-args constructor, and so you cannot
create an instance with newInstance().
In this specific case you could declare MyClass to be a static member, in which
case its instances would not have outer objects, and so there would be a
no-args constructor just as it appears. That wouldn't solve the general
problem, which is that you cannot in general rely on classes having no-args
constructors. Any solution to that problem is necessarily
application-specific.
-- chris
Bjorn Abelli - 06 Nov 2005 13:59 GMT
"zero" wrote...
> I'm trying to figure out reflection, and I've run into
> a problem. In the code below the JFrame and String get
> instantiated fine, but MyClass gives
> an InstantiationException.
>
> What am I overlooking here?
You should consider an inner class as a part of the definition of the outer
class.
In this case you can't instantiate any MyClass at all outside of an instance
of class ReflectionTest, as it's defined as non-static.
To resolve it, you could either move the definition of MyClass outside
ReflectionTest, or making MyClass static.
// Bjorn A
Benji - 06 Nov 2005 18:27 GMT
> I'm trying to figure out reflection, and I've run into a problem. In the
> code below the JFrame and String get instantiated fine, but MyClass gives
> an InstantiationException.
> What am I overlooking here?
> <snip>
As the other posters said, since the class is internal, it actually has a
constructor that takes in an object of the parent type. You can actually
instantiate it, though. Here's an example I just wrote. You can modify
it to fit your needs:
import java.lang.reflect.Constructor;
public class Outer
{
public static void main(String[] args) throws Exception
{
Class innerClass = Inner.class;
Constructor cons = innerClass.getConstructor(new Class[]{Outer.class});
System.out.println(cons.newInstance(new Outer()));
}
public class Inner
{
public String toString() { return "hello, world!"; }
}
}

Signature
Of making better designs there is no end,
and much refactoring wearies the body.
zero - 06 Nov 2005 19:16 GMT
>> I'm trying to figure out reflection, and I've run into a problem. In
>> the code below the JFrame and String get instantiated fine, but
[quoted text clipped - 25 lines]
> }
> }
Yep that does it :-)
Thanks everyone for replying