aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor 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 "constructor" Watch "constructor" New topic
Author

constructor

meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
Hi all

If we invoke a inner class using the instance of the outer class does the outer class constructor will also get invoked or not....

For example

Can anybody explain how many time A() is invoked.

Thanks In advance.
[ February 23, 2005: Message edited by: ramya jp ]
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Once.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Ramya,
Simple test would be to put System.out.println statement in the constructor of the Outer class and observe the output.


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Ramya, you may like to try this variation of your code:


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
Hi all.
When u dont extend the class A then

line 1 will call the constructor of A.
Line 2 will invoke the constructor of B using an instance of A.
Line 3 does the same

But when we make
class B extends A
Then each time when we invoke the B() the superclass Constructor is also call.

I am not clear why this difference happens.Is it because only when we extend the B becomes the subclass of A.
If we dont extend then B is just a class inside a Class A and not a subclass.
Thanks in advance
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Ramya wrote:
Is it because only when we extend the B becomes the subclass of A?
If we don't extend then B is just a class inside a Class A and not a subclass?


Exactly.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: constructor