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

Constructor Chaining

deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
Hi,
To all,Ranchez could anny buddy clear my doubt the execution
of constructors.ds can b done using this() and super().
Ex

There is Line written in Book Khalid & Mugal.dt Body of contructor is executed in reverse order.preety confusing plz help out

(added the tags for formatting code to look like code sort of)
[ March 24, 2005: Message edited by: Barry Gaunt ]

"Do not be afraid of going slow, be afraid of standing still"
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

First off, under the Add Reply button are ten buttons. One of them is the CODE button which will put CODE tags in your post, where you can paste your code in there and it will remain formated, it will keep its indentation which makes things easier to read.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

It prints in reverse order because it calls another constructor first before it does the printouts.

So if you call the no args constructor, it first calls the two arg constructor, which then calls the three args constructor, runs 3's printout, then returns to the 2 arg constructor to its printout of "2", then back to the no args constructor, and there it prints its "1"

Hence, 3 2 1.

Mark
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
I don't think your code compiles, the instance field "indicator" cannot be referred to when you call a constructor of the class:

this(watts,indicator,"X");

Your code would compile when "indicator" is static but that is probably not the intention.
[ March 24, 2005: Message edited by: Thomas De Vos ]

Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
Thanks Mark thanks for ur guidance,I will take care of posting
and replying problems from now.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor Chaining