• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Problem with Class.forName()

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, all!

There is a question.
I have the classes:



I want to create the instance of Inner class using name of inner-class ("Inner") and the instance of Outer.
Like this:

Object obj = Class.forName("Outer$Inner"); - this code is valid only if Inner class is static. But it is not static in my case.

Thanks all
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to create the instance of Inner class using name of inner-class ("Inner") and the instance of Outer.



Since the inner class is not static, you can't create an instance of it, without an instance of the Outer class. Now, if it doesn't matter what the instance of the Outer class is, then I guess you can create an instance of an outer class too...



Henry
 
Simeon Cherniy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be I wrote not clear:

I have:
1. Outer out = new Outer();
2. String str = "Inner";
3. Outer.Inner in = null;

I want:
in = an instance of inner class.

Suppose you don't know about is there inner-class with name "Inner" or not. And you can't write new Outer().new Inner()

Thanks!
 
Simeon Cherniy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have resolved this problem.
The problem was, that constructor (by default) of inner class required an instance of outer one
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic