This week's book giveaway is in the Game Development forum.
We're giving away four copies of Badass: Making Users Awesome and have Kathy Sierra on-line!
See this thread for details.
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

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
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 = 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 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(;//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: 1707
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:
subject: Inner class doubt