Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor Chaining

 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark thanks for ur guidance,I will take care of posting
and replying problems from now.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic