I’m curious as to the syntax choice for instantiating an inner class given an instance of the outer class in Java.
The syntax is:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
But why is it not:
OuterClass.InnerClass innerObject = new outerObject.InnerClass();
The former seems to imply that new is a method or operator directly associated with the class, but my understanding is that this is not the case (unlike C++)?
The latter would imply to me that the type name was
outerObject.InnerClass– whereas actually the typename is justInnerClass(orOuterClass.InnerClass, which would also be legal) constructed with relation to the instance referred to byouterObject.Personally I don’t like the way Java does nested classes in the first place, and I agree it looks a little bit odd, but I can see why it’s done that way.