Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Origin of Java syntax for creating new instance of an inner class?

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++)?

like image 611
Uri Avatar asked May 03 '11 14:05

Uri


1 Answers

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.

like image 164
Jon Skeet Avatar answered Oct 19 '22 23:10

Jon Skeet