I am trying to refer to the class a nested class is 'in'... The 'this' keyword refers to the nested class, so I need something like super.this (except not at all like that) :/

I think the code below describes what I am trying to do:

public class Nest {
   
    private String var = "foo";
    private MyOtherClass myOtherClass;

    private class Nested {
        public doSomething() {
            //Here I want to access the class this is nested in,
            //now this should be really easy (i can access the
            //variable foo) but 'this' is for the Nested instance
            myOtherClass = new MyOtherClass(######);
            //what do I need to put here: ###### to refer to the Nest?
        }
    }  
}

public class MyOtherClass{
   
    private Nest parentNest;

    public MyOtherClass(Nest nest){
        parentNest = nest;
    }  
}

Can anybody help? ^_^

Recommended Answers

All 3 Replies

you should put Nest.this like below

myOtherClass = new MyOtherClass(Nest.this);

commented: answerd it in two lines! +1
Nest nest = new Nest();
myOtherClass = new MyOtherClass(nest);

This creates an instance(object) of your Nest class.

you should put Nest.this like below

myOtherClass = new MyOtherClass(Nest.this);

Well, it was as simple as I thought it should be! Its a real pain when you know exactly what you want to do but are unsure of how to search for the answer.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.