Question

Consider the following nested classes.

class Outerclass {

    class innerclass {

    }

}

 class util {

 //how to declare an array of innerclass objects here?
}
Was it helpful?

Solution

You can declare an array of innerclass objects like this.

class util {
    Outerclass.innerclass[] inner = new Outerclass.innerclass[10];
}

And to instantiate them you can do something like this inside the util class.

void test() {
    Outerclass outer = new Outerclass();
    inner[0] = outer.new innerclass();
}

OTHER TIPS

OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerArray[] = new OuterClass.InnerClass[3];
// Creating Objects of Inner Class
OuterClass.InnerClass innerObject1 = outerObject.new InnerClass();
OuterClass.InnerClass innerObject2 = outerObject.new InnerClass();
OuterClass.InnerClass innerObject3 = outerObject.new InnerClass();
// Adding the Objects to the Array
innerArray[0] = innerObject1;
innerArray[1] = innerObject2;
innerArray[2] = innerObject3;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top