Solved

Get Reference to Parent Class

Posted on 2009-04-13
1
2,156 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
1 Comment
 
LVL 92

Accepted Solution

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

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

808 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