• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Inner class... how and why ?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have some questions, not on syntax but more on 'how and why' ?
- Why would one want to instanciate an inner class before instanciating the outer class ?

- When instanciating an outer class, is the inner being instanciated as well (automatically) If 'yes' why does the language enbales instanciation of an inner independently of it outer class ?
Please give me some example (short)...
Thx
Pascal
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check out the campfire story about Inner class. http://www.javaranch.com/campfire.jsp
Easier way to understand about Inner classes.
-zarina
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't instantiate an inner class prior to instantiating the enclosing class. All inner classes must have an enclosing instance in order to exist. The inverse is not true. You do not need to instantiate an inner class to instantiate an outer class. By that, I mean that, if an outer class has a reference to an inner class, that reference can remain null after the outer class is instantiated.
You can, however, create an instance of a top-level nested class (which is like an inner class, but it is static, so it's not truly an inner class) without an instance of the enclosing class.
I hope that helps,
Corey
 
No, tomorrow we rule the world! With this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic