Solved

Get Reference to Parent Class

Posted on 2009-04-13
1
2,161 Views
Last Modified: 2012-05-06
If you have a nested class inside a parent class, the 'this' keyword will of course refer to an instance of the nested class when used inside the nested class.  But is there any way to get a reference to an instance of the parent class, without passing one over to the constructor of the nested class?  
public class Parent
{
    class Nested
    {
        void doSomething()
        {
            Parent p = this; // <--- doesn't work because 'this' refers to an instance of Nested
        }
    }          
}

Open in new window

0
Comment
Question by:chsalvia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 24134387
           Parent p = Parent.this;

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question