• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Creation of a Nested Top-level class

 
Deepak Kaul
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to load a nested top-level class dynamically?

I trying doing Class.forName of a nested top-level class and it gives a ClassNotFoundException.

Has anyone tried this?
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's a "nested top-level class"? Perhaps you are wanting to load an inner class. If so, you can do so with Class.forName(), but you must separate the outer class name from the inner class using a dollar-sign, not a dot.

For example, if Inner is an inner class defined in class org.pete.Outer, you can load it with Class.forName("org.pete.Outer$Inner").
 
Deepak Kaul
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter, your reply helped.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Peter]: What's a "nested top-level class"?

That's the term Sun originally used to describe a static member class (according to the badly-named "Inner Classes Specification"). It was a very confusing choice, and fortunately Sun did away with the term when the Java Language Sepcification 2nd edition came out. (Top-level and nested are now mutually exclusive, as they should be based on what they mean in English.) They didn't really announce this change, but just did it. So you will still occasionally encounter books and people using the old term.

[Peter]: Perhaps you are wanting to load an inner class.

Actually it's a nested class, but not an inner class. The solution of using $ as the separator still applies though. Documented here.
[ November 08, 2005: Message edited by: Jim Yingst ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic