• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Class.forName()  RSS feed

 
Simeon Cherniy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!