This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Dynamic Creation of a Nested Top-level class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamic Creation of a Nested Top-level class" Watch "Dynamic Creation of a Nested Top-level class" New topic
Author

Dynamic Creation of a Nested Top-level class

Deepak Kaul
Greenhorn

Joined: Nov 08, 2005
Posts: 13
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

Joined: Oct 30, 2001
Posts: 1970
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").


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Deepak Kaul
Greenhorn

Joined: Nov 08, 2005
Posts: 13
Thanks Peter, your reply helped.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic Creation of a Nested Top-level class