Consider the following nested classes.
class Outerclass {
class innerclass {
}
}
class util {
//how to declare an array of innerclass objects here?
}
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();
}
innerclass. Won't you have to initialize each element in the array? If it was a primitive data type like int, all the elements would be filled with 0 by default. But in this case, it'd be null. All the array elements needs to be initialized. You've just declared and initialized the an array, but have not initialized the elements in the array.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;