Search code examples
javaclasssyntaxinner-classesnested-class

Create array of inner class object in a different class


Consider the following nested classes.

class Outerclass {

    class innerclass {

    }

}

 class util {

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

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();
    }