0

I'm very new to Java. Let's say I have something like the following:

public class OuterClass {
    ...
    ...
    public class InnerClass {
        ...
    }

    public static void main(String[] args) {

    }
}

How would I declare an array of type InnerClass in main, if it is nested in OuterClass.This does not work.

InnerClass[] innerClassArray = new InnerClass[size] 

Thank you.

3
  • What is the relationship between Person and InnerClass and OuterClass? Commented Jan 22, 2016 at 1:25
  • Sorry I was working on something else and did that by accident.... I edited my post. Commented Jan 22, 2016 at 1:26
  • This might be helpful stackoverflow.com/questions/21299833/… Commented Jan 22, 2016 at 1:34

1 Answer 1

0

An instance of InnerClass can't exist except as part of an instance of OuterClass and the class definition of InnerClass is tied to the class definition of OuterClass.

More correct would be:

Outerclass.InnerClass[] innerClassArray = new Outerclass.InnerClass[size]; 

And to populate it:

OuterClass outer = new OuterClass();
innerClassArray[0] = outer.new InnerClass();
innerClassArray[1] = outer.new InnerClass();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.