Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes inner class & nested class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inner class & nested class? " Watch "inner class & nested class? " New topic

inner class & nested class?

Yuan Tseng

Joined: Mar 11, 2003
Posts: 21
What is the difference between inner class & nested class? Please an example.
Thanks in advance.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
A nested class is *ANY* class that is within the scope of another class...
class Foo {
class Bar {
An inner class just means that the nested class is *not* static.
In the old days, when inner classes were first put into the language (starting with version 1.1 -- we didn't have them in version 1.02 of Java), they were just called "inner classes" and even-- although considered a BIG faux pas today-- "static inner classes". You will also still here old-timers (like me) say "top-level nested class" which was one way of describing a static inner class. But now, both the term "static inner class" and "top-level nested class" are not correct.
A static nested class is a static nested class, and an 'inner' class is a non-static nested class. Both, though, are under the heading 'nested class'.
Then of course you have anonymous inner classes... and they cannot possible *be* static, so they're always inner classes as well.
So that's the terminology difference. The behavioral difference is that an inner class is *always* tied to an instance of the enclosing class; they share an intimate relationship (you can read the campfire story about this). But a static nested class does NOT become 'tied' to any particular instance of the outer class, and in fact there need not BE any instances of the outer class floating around. That has serious implications, because the static nested class then does not have access to any instance variables of the enclosing class, the way an inner class does.
Static nested classes aren't much more than a way to limit the scope of a class -- often a namespace consideration. Whereas inner classes are there so that an instance of the inner class can work as a helper for an instance of the outer class. The best way to see the benefits of inner classes, if you're just starting out, is to look at GUI even listeners.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
The following nitpick is probably outside the scope of "Java in General (beginner)", so feel free to skip it - but in the interest of presenting a more accurate answer:
The behavioral difference is that an inner class is *always* tied to an instance of the enclosing class
Not quite. A static nested class is *never* tied to an instance; however an inner class *may* be tied to an instance. It depends on where the class is declared. If you declare an inner class inside a static method, then you're not allowed to use "this" at all (because you can never use "this" inside a static method) - and thus there's no way to refer to an associated outer class instance. There's no instance available inside a static method. This is called declaring an inner class in a static context. It's sorta similar to a static nested class in some ways (because there's no instance), but it's not the same thing. An inner class in a static context is not considered a static class, and so it's still called an inner class - even though it has no instance. It obeys other rules of an inner class though, such as "inner classes may not declare static members, unless they are compile-time constant fields". (So yes, an inner class in a static context is different from a static nested class.) Details may be found in the JLS under "Inner classes and enclosing instances".

"I'm not back." - Bill Harding, Twister
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Ohhhh Jim brought up a good one. And he's right that it isn't a static nested class. So I guess technically it *is* an inner class even though it's within a static context. But I'm still gonna think of that particular flavor of inner class as a special case, because I don't *like* that use of them, but it's perfectly legal and has a value.
So, no, Jim I don't think it's a nit-pick.
I was just in denial
I agree. Here's the link:
subject: inner class & nested class?
Similar Threads
nested class
inner class
Static Inner Class Challenge
Inner Class and Nested Class
no static method in inner class - why ?