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

technical point about inner classes

Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
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


Sun Certified Programmer for the Java 2 Platform
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
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

Joined: Jul 11, 2001
Posts: 14112
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:


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
Thanks for the clear explanation.
Edward Brode
Greenhorn

Joined: Apr 30, 2002
Posts: 11
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


Edward Brode<br />SCJP2<p>"It works on my machine"<br /> - Paul Symonds
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Apr 30, 2002
Posts: 11
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Apr 30, 2002
Posts: 11
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Apr 30, 2002
Posts: 11

Ah, yes, thanks Sheriff!
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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
 
subject: technical point about inner classes