Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

reflection InstantiationException

Thread view: 
zero - 06 Nov 2005 13:28 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?

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.