• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

innerclasses

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
apart from instantiating the innerclass
using this: outer.inner i = new outer().new inner(); outer o = new outer();
outer.inner i = o.new inner();can u also do this?
outer o = new outer();
inner i = o.new inner();?
it doesn't sit well with me can anyone help?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything there looks just fine. The key to remember is that you need an instance of an outer class before you can instantiate an inner class for it. When you run into a problem like this, you might just want to write a test script, like this:

As you can see, I simply commented out certain lines so that I could try different combinations to ensure that each combination would compile.
Corey
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class outer1{
private static class inner {}
public static void main(String[] args) {
outer1.inner i = new outer1().new inner();
//outer1 o = new outer1();
//outer1.inner i = o.new inner();
//outer1 o = new outer1();
//inner i = o.new inner();
}}
Can u explain why this gives runtime error. Pls note i have made the inner class static.
Also i have read that non-static inner class do NOT need the enclosing class instance to be instantiated. Can u explain this with example?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepali Pate:
...Pls note i have made the inner class static...


No, you haven't. By using the keyword static, you no longer have an inner class. Instead, you now have a top-level nested class. In order to instatiate it, you'd use something like this:

Notice that no instance of the outer class is created.
Be sure to look at the JLS, §8.1.2 Inner Classes and Enclosing Instances.
Corey
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic