• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

technical point about inner classes

 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From an inner class you can call a non-static method belonging to the enclosing class, without any fanfare.
But how does an instance of an inner class know which instance of the outer class it belongs to?
Does it resolve to:
1) The instance which created it?
2) An instance which has it as an instance variable?
Is a reference to the enclosing instance stored in the inner class; and if so, how is this reference initialized? Or is the reference found by some other mechanism?
Thanks
Geoffrey
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that you cannot create an instance of a non-static inner class without an outer class instance. Each instance of an inner class is implicitly associated with an outer class instance -- the outer class reference can be obtained via Outer_class_name.this
I'm not sure if this is what you were asking for?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Geoffrey Falk:
But how does an instance of an inner class know which instance of the outer class it belongs to?
Does it resolve to:
1) The instance which created it?
2) An instance which has it as an instance variable?

It's 1)
The inner class has a reference to the instance of the outer class which created it.
For example, if you have a class OuterClass and an inner class OuterClass.InnerClass, you can access the OuterClass inside from within the InnerClass by using

Notice though, that you *can* instantiate the inner class outside of the outer class, by using an instance of the outer class:
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clear explanation.
 
Edward Brode
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also refer to the inner class without an explicit name for the outer class as in:

However, in neither case is the inner class actually *outside* the outer class. There always has to be an enclosing class instance, but you don't have to keep a reference to it.
It's also important to remember that we are talking about member inner classes, not local inner classes that are declared inside a method like an instance of an ActionListener, etc. in the AWT, among other places.
Edward
SCJP
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edward Brode:
It's also important to remember that we are talking about member inner classes, not local inner classes that are declared inside a method like an instance of an ActionListener, etc. in the AWT, among other places.

Doesn't a local class also have a reference to outer classes instance?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually it does - but not if it's declared in a static context (e.g. in a static method). As long as a local or anonymous class is declared in a non-static context, it will get a reference to its enclosing instance, the same way a nonstatic member class would.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Usually it does - but not if it's declared in a static context (e.g. in a static method).

Ah, yes - that makes sense...
Thanxs!
 
Edward Brode
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I guess the reason that statement about local inner classes seemed pointless, was because I forgot where I was going with it. I meant it merely as a reminder that although a local inner class automatically gets a reference to the outer class, it doesn't automatically get access to local variables declared inside the same method, it can reference them only if they are declared as final.
Also, I don't think there is any way to declare instances of local inner classes, anonymous or otherwise, without an explicit enclosing outer class, method and all.
Edward
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I don't think there is any way to declare instances of local inner classes, anonymous or otherwise, without an explicit enclosing outer class, method and all.
Ummm... Any nested class has an explicit outer class, bu definition. But it doesn't necessarily have an outer class instance. E.g. if it's inside a static method - there's no outer instance. But there is an outer class.
 
Edward Brode
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, so it appears. I tried to create one like this:

But this produces output that indicates the Outer class constructor is being called, which I think means the Outer class is being instantiated. So after I compiled the code, I looked at the name of the Nested class .class file which is
Outer$1$Nested.class and changed the main method to:

This seems to me to be cheating, in some way, but it makes the point. Also, this will only compile if the previous code is compiled first, otherwise the compiler can't find the .class file with the $ signs in its name.
Just to make certain something sneaky wasn't going on, I also put the same main method in another class:

I would certainly like to see a practical use for a stand alone instance of a local nested class.
Edward
[ May 27, 2002: Message edited by: Edward Brode ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this produces output that indicates the Outer class constructor is being called, which I think means the Outer class is being instantiated.
That's because your main() method is calling new Outer(). You're explicitly instantiating Outer. But since staticOuterMethod() is, in fact, a static method, you don't need an instance to call it - you can just use the class name:
 
Edward Brode
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ah, yes, thanks Sheriff!
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply put. if you decompile the code you will see this for the creation of the inner class
innerclass(outerclass.this)
so when you do this
innerclass ic = new innerclass();
their is a hidden parameter which is passed in the constructor which is a reference to the outer class.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic