Reputation: 105
I have the following piece of code:
public class InnerClassStuff {
public class A{
public class AA{}
}
}
my question is how can I instantitae an AA object?
I've tried the following but it won't compile:
public static void main(String[] args){
InnerClassStuff object = new InnerClassStuff();
A a = object.new A();
AA aa = object.a.new AA(); //error
}
Upvotes: 0
Views: 239
Reputation: 73
To access the class you have to use The outer class name, only by doing so you can have reference variable of inner class. eg:
InnerClassStuff object = new InnerClassStuff();
InnerClassStuff.A a = object.new A();
InnerClassStuff.A.AA aa = a.new AA();
Upvotes: 0
Reputation: 21961
To instantiate an inner class, you must first instantiate the outer class. So, you can't declare A a= ..
, you need wrapped it with outer class like below:
InnerClassStuff object = new InnerClassStuff();
InnerClassStuff.A.AA a = object.new A().new AA();
Or,
InnerClassStuff object = new InnerClassStuff();
InnerClassStuff.A a = object.new A();
InnerClassStuff.A.AA aa = a.new AA();
Upvotes: 1