0

so i have outerClass and innerClass, and i want to access the object created from innerClass using outerClass, example:

public Class outerClass{
    Class innerClass{
        //properties
    }
}

so what i want to do is something like this:

public class Main {
    public static void main(String [] args) {
        outerClass outerObj = new outerClass();
        outerClass.innerClass innerObj = outerObj.new innerClass();

        //this is what i want:
        outerObj.innerObj;
    }
}

it might be complicated but what i want to do is get the innerObject, using only outerObject

3
  • So what is the problem ? Commented Mar 11, 2016 at 12:43
  • line below comment doesnt work Commented Mar 11, 2016 at 12:46
  • Create innerObj in your outerClass. Commented Mar 11, 2016 at 12:46

4 Answers 4

3

This would work:

public class outerClass{
    public static class innerClass{
        //properties
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I thnk it is a valid answer
1
public Class outerClass{
    Class innerClass{
        //properties
    }

    public innerClass innerObj;
}

outerObj.innerObj = outerClass.new innerClass();

6 Comments

yes that's the solution i came up with but is there any easier way?
@nikagar4 What do you mean by easier ?
To be able to perform outerObj.innerObj there is no other way
is there a way to do that without that extra property called "innerObj"?
@nikagar4 Then you shouldn't say and i want to access the object created from innerClass using outerClass. Create a separate class.
|
1
outerClass.innerClass innerObj = outerObj.new innerClass();

innerObj is a local variable. You can refer to it directly as

innerObj

4 Comments

i want to access it from another class
Then you'd need to pass it to the other class. It's just a plain old local variable. outerObj doesn't keep a hold of a reference to innerObj (but innerObj does keep hold of a reference to outerObj).
yes i understand but i wanted to be able to access it using only outerClass
@nikagar4 then you don't understand. You can't access it using only outerClass because outerClass (or outerObj) doesn't keep a reference to innerObj, unless you actually add a reference to the instance to the outer class, like Nadir suggests.
1

For a non-static inner class, the compiler automatically adds a hidden reference to the "owner" object instance. When you try to create it from a static method (say, the main method), there is no owning instance. It is like trying to call an instance method from a static method - the compiler won't allow it, because you don't actually have an instance to call.

So the inner class must either itself be static (in which case no owning instance is required), or you only create the inner class instance from within a non-static context.

So make your innerClass static

public static class innerClass{
        //properties
    }

You can call

outerClass.innerClass innerObj = new outerClass.innerClass();

If you want to access fields only via dot(.) notation should consider using

static inner field in your outerClass like a PrintStream in System class

Example:

public class outerClass{
    public static innerClass innerObject = new innerClass();
    static  class  innerClass{
            //properties
        }
    }

and then in main method

outerClass outerClass= new outerClass();
        outerClass.innerObject.yourfied

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.