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

nikagar4
  • 830
  • 4
  • 11
  • 23

4 Answers4

3

This would work:

public class outerClass{
    public static class innerClass{
        //properties
    }
}
Pragnani
  • 20,075
  • 6
  • 49
  • 74
Prashant
  • 4,775
  • 3
  • 28
  • 47
1
public Class outerClass{
    Class innerClass{
        //properties
    }

    public innerClass innerObj;
}

outerObj.innerObj = outerClass.new innerClass();
Nadir
  • 1,799
  • 12
  • 20
1
outerClass.innerClass innerObj = outerObj.new innerClass();

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

innerObj
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • i want to access it from another class – nikagar4 Mar 11 '16 at 12:50
  • 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`). – Andy Turner Mar 11 '16 at 12:50
  • yes i understand but i wanted to be able to access it using only outerClass – nikagar4 Mar 11 '16 at 12:53
  • @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. – Andy Turner Mar 11 '16 at 12:54
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
nikagar4
  • 830
  • 4
  • 11
  • 23
Pragnani
  • 20,075
  • 6
  • 49
  • 74