• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Where Can I Read about Non-Static Inner Classes?

 
Ranch Hand
Posts: 231
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've made a lot of use with static inner classes. I'm curious about what really goes on with an inner class that isn't static. I know that I can, inside a static inner class, refer to a member of the outer class, either a variable or a method. But that kind of confuses me. Can anyone think of somewhere I can go to read what actually happens when an inner class refers to a member of the outer class? I'm aware of the possibility that the Java Docs may discuss this, but I have no idea where to go in the Java Docs to find such a discussion.

Kevin Simonson
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All inner classes are non-static. The definition of an inner class is, roughly, "a non-static nested class," although last time I dug into it there seemed to be a bit of ambiguity or inconsistency in the JLS on that distinction.

Anyway It's not clear exactly what you're asking.

If you want to know the technical details of how it's represented, there's the JVM spec: http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#79996

If you want the formal definition and rules, there's the JLS: http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.1.3
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oracle's Java Tutorials have some page about nested classes and inner classes.

One thing to be aware of is this: an instance of an inner class is associated with an instance of its enclosing class. (That's also why you can access member variables from the enclosing class inside the inner class).

When you create a new instance of the inner class in a constructor or non-static method of the enclosing class, the enclosing class instance that the inner class instance will be associated with is the 'this' of the enclosing class.

When creating a new instance of the inner class in a static context (for example, a static method of the enclosing class), you must explicitly indicate the instance of the enclosing class that the inner class instance must be associated with. You do this by calling the new operator for the inner class on an instance of the enclosing class.

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic