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 just InnerClass (or OuterClass.InnerClass, which would also be legal) constructed with relation to the instance referred to by outerObject.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With