wood burning stoves 2.0
The moose likes Java in General and the fly likes Where Can I Read about Non-Static Inner Classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Where Can I Read about Non-Static Inner Classes?" Watch "Where Can I Read about Non-Static Inner Classes?" New topic

Where Can I Read about Non-Static Inner Classes?

Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 123
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

Joined: Jan 03, 2004
Posts: 6109

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

Joined: Aug 16, 2005
Posts: 15081

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link: http://aspose.com/file-tools
subject: Where Can I Read about Non-Static Inner Classes?
It's not a secret anymore!