aspose file tools*
The moose likes Java in General and the fly likes Confused with inner and nested 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 "Confused with inner and nested classes" Watch "Confused with inner and nested classes" New topic
Author

Confused with inner and nested classes

Manfredo Kopfinger
Greenhorn

Joined: May 15, 2006
Posts: 7
Hi!

What I don't really get is the way that inner classes and nested classes work, the whole thing seems a bit inconsistent to me.

For example take the following code:



In this example you can access the variable i inside a static method in the outer class without creating an object of the outer class, even though the Inner class is a non-static member of the outer class. Isn't that strange?

Furthermore,you can also access i in a non-static method of the outer class like this:



but funnily enough you can't do the same from Main:



Can somebody tell me why this examples behave the way they do?
Probably somebody can provide some technical background regarding the Inner Classes that make it clear why they behave like they do.
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Did you try to make Inner public? I think Outter can access your variable because it has access to Inner. But since Inner not public, nobody else has access to its internals.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Visibility isn't the issue. You should be accessing it using Outer.Inner.i I believe.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Consider that within the context of Outer, Inner is simply a class with a static variable called i. So within the context of Outer, Inner.i is a perfectly valid reference, regardless of whether it appears in a static context or not.

Note that in the static context of main, Inner.i works the same as it does in any other static method of Outer, so this really is consistent.

Because it's an inner class (meaning a non-static nested class), any instance of Outer.Inner (not just Inner) must be associated with an instance of Outer. So if you wanted to use instance references, you could use...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


Because it's an inner class (meaning a non-static nested class), any instance of Outer.Inner (not just Inner) must be associated with an instance of Outer. So if you wanted to use instance references, you could use...


My compiler doesn't agree.
From main, I can call:


i is static, so we don't need a Inner-instance.
If we don't need a Inner-instance, why should we need a Outer-Instance?


http://home.arcor.de/hirnstrom/bewerbung
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I agree with Ken. I don't see what the fuss or concern is about. i is a class constant of class Outer.Inner, so from classes like Main you access it by prefixing it with its class name:

System.out.println(Outer.Inner.i);

This seems perfectly intuitive.


There is no emoticon for what I am feeling!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Stefan Wagner:
...From main, I can call:


i is static, so we don't need a Inner-instance.
If we don't need a Inner-instance, why should we need a Outer-Instance?

Right. In this context, "Outer.Inner.i" can certainly be used in place of "Inner.i." And because i is static, you certainly don't need any instances. But because of the original confusion using an instance of Outer ("ou"), I was just saying "if you wanted to use instance references..." (But that's a big "if.")
 
jQuery in Action, 2nd edition
 
subject: Confused with inner and nested classes