aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class question" Watch "inner class question" New topic
Author

inner class question

Ian Wu
Greenhorn

Joined: Jul 13, 2003
Posts: 9
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Manvir Singh
Greenhorn

Joined: Oct 19, 2003
Posts: 6
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

Joined: May 05, 2000
Posts: 13974
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

Joined: Apr 23, 2002
Posts: 168
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?
 
Consider Paul's rocket mass heater.
 
subject: inner class question