• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Dynamic Creation of a Nested Top-level class

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter, your reply helped.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic