Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

InstantiationException with newInstance() when creating an object of an inner class

Posted on 2005-03-16
8
Medium Priority
?
1,029 Views
Last Modified: 2008-01-09
Hi all
I am trying to use Class.forName().newInstance() to create objects from names.
The class is created ok (no ClassNotFoundException) but the newInstance() fails
with InstantiationException.

The class is a non-static non-abstract inner class of an inner class with a simple constructor.

          String className = "com.menatwork.gui$MultiDialog$anotherDialog";
          Class clazz = Class.forName(className);
          // fails here
          Object ob = clazz.newInstance();

Is there a problem creating objects from inner classes or am I barking up the wrong tree?

Thanks

Rich
0
Comment
Question by:richard_fairall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13553170
what does your inner class look like?
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 13553181
depending on what constructors you have defined, you could try something like:

Constructors[] ctors = clazz.getDeclaredConstructors();
Object obj = ctors[0].newInstance(new Object[] {this});
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13553188
>> The class is a non-static non-abstract inner class of an inner class with a simple constructor.

Are you sure about each one of that?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 13553190
Since its a non-static inner class, I guess you would need an object of the enclosing class to instantiate it. You can't instantiate an object of a non-static inner class without having an object of the outer class.
0
 
LVL 92

Expert Comment

by:objects
ID: 13553234
> I guess you would need an object of the enclosing class to instantiate it.

Thats correct, and the reason for passing 'this' to the ctor in the code I posted above.
0
 

Author Comment

by:richard_fairall
ID: 13553255
Thanks objects,
So fast - unbelievable.
I dont know what country you are in, or what time it is there, but thanks for being up!
That worked a treat.

Rich
0
 
LVL 92

Expert Comment

by:objects
ID: 13553266
Sydney, Australia.
And it's about beer o'clock here, so you got me jit :)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13553275
Assuming you are using that code in the enclosing class ;) good that it worked.
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question