• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class question

 
Ian Wu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have trouble understanding why the following code could compile fine

The Output is: asdf
at position #1, the innerclass is instantiating as if the inner class is a static nested class, but clearly it isn't, it is only a regular inner class. How is this possible?
Thanks in advance for helping
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, "Outer" is not required. The program works without it. Second, what is the actual name of the Inner class? It is Outer.Inner. So all you are saying is instantiate an Inner object using it's qualified name. This would not work from any context other than inside the Outer class.
 
Manvir Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
This is a real goof up on the syntax, or the already-weird syntax of Inner Classes.
Inside the callingInside () Method if I use
this.new Inner().hello();
I get the same output. And according to Kathy and Bates I shud. Bartenders, any lights on this???
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manvir Singh:
Hi!
This is a real goof up on the syntax, or the already-weird syntax of Inner Classes.
Inside the callingInside () Method if I use
this.new Inner().hello();
I get the same output. And according to Kathy and Bates I shud. Bartenders, any lights on this???

Again, what is the name of the inner class? Remember that "this" represents an instance of the Outer object. If "a" was an instance of the Outer object then wouldn't this be legal:
a.new Inner().hello();
So why shouldn't I be able to replace "a" with "this" as long as I am in an Outer context?
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see why you think that there is anything static about #1. Nothing is static except the main() method.

I guess the confusion is that is invoked via the name of the class instead of the name of an object. So it looks kind of like a static invocation.
But the same is true for all constructors. What would a non-static constructor invocation look like?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic