jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Subclass of inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Subclass of inner class" Watch "Subclass of inner class" New topic

Subclass of inner class

Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Here's an obscure Java question that I hope one of you can answer:
At the top level, I have defined a subclass of an inner class. From a method in that subclass, is there any way to refer to the outer class object or to the outer class object's variables and methods?
In the code below, the method dostuff() is written identically in Inner and SonOfInner. It shows a variety of ways to access methods and data in the outer class -- but all of them fail for the SonOfInner class. Is there any way to do this at all?

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Afaik, there isn't. I think you do have a serious design problem here. Why does SonOfInner need to access Outer.this? Do you have a real world example where you need to do this?

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
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
This is not a real-world example. I am just starting to learn Java, and noticed that it is possible to declare, at the outer level, a subclass of an inner class.
I'm trying to understand how things work, and doubt that I would ever do this in "real life".
I agree. Here's the link: http://aspose.com/file-tools
subject: Subclass of inner class
It's not a secret anymore!