Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Problem with Class.forName()  RSS feed

 
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
 
author
Sheriff
Posts: 23566
138
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!