reflection InstantiationException

Z

zero

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";
}
}
}
 
C

Chris Uppal

zero said:
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
 
B

Bjorn Abelli

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
 
B

Benji

zero said:
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!"; }
}
}
 
Z

zero

zero said:
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!"; }
}
}

Yep that does it :)

Thanks everyone for replying
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top