Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where Can I Read about Non-Static Inner Classes?

 
Kevin Simonson
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic