Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused with inner and nested classes

 
Manfredo Kopfinger
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Visibility isn't the issue. You should be accessing it using Outer.Inner.i I believe.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.")
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic