wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class doubt" Watch "Inner class doubt" New topic

Inner class doubt

Neha Monga
Ranch Hand

Joined: Mar 13, 2007
Posts: 34
Hello Ranchers!

ouput --> Hello

Pls explain why the output is NOT -->

Appreciate your help!

Neha Monga
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447


In inner = o.new In();

When the jvm executes this statement, the default constructor of the Out will be executed because in extends Out.There you are setting the id="Default",And in the inner class if you simply say id ,means this.id.Because In extends Out the id is inherited to the subclass because it is not declared as private.

This is a specaial syntax to get refernce to the top level class.
System.out.println(Out.this.id);//refers to

Out o = new Out("Hello");//line 1
In this the default constructor won't run.

Anil Kumar

[ May 31, 2007: Message edited by: anil kumar ]
[ May 31, 2007: Message edited by: anil kumar ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710
Hi Neha,

Adding to what Anil said:

If you get how many times the no-arg constructor of class Out is called,
you can make yourself doubt clear.

I agree. Here's the link: http://aspose.com/file-tools
subject: Inner class doubt
It's not a secret anymore!