This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Here're are two rules relating to constructors. Remembering these rules makes things more predictable. 1. Every class has a constructor. If the programmer doesn't explicitly code a constructor, then Java creates a "no args" constructor for you. 2. The first line of a constructor must be a call to another constructor via super([param list]) or this([param list]). If the programmer doesn't explicitly code the call to another constructor, then Java inserts the line "super()" for you. Unfortunately, Java uses the key words "this" and "super" in two different ways. The syntax this([param list]) and super([param list]) is used to reference another constructor. This syntax can only be used as the first line of a constructor. For example, "this(17);" as the first line of a constructor is a call to another constructor within the class that takes an int parameter. If there is no constructor within the class that takes an int parameter then you get a compile error (just like you'd get whenever you try to call a routine that doesn't exist). The statement "super();" as the first line of a construcor is a call to the constructor in the super class that has a "no args" parameter list. Again, if there is no such routine in the ancestor then you get a compile error. The reason Java does all this is to guarantee that constructors are called up the class hierarchy to Object -- i.e., the Object constructor always eventually gets run when you create any object. In every class at least one constructor must call "super". (The compiler should check for this, but it doesn't.) Besides that one call to the super class constructor, the other constructors can use "this" -- using "this" allows you to have one constructor do all the work, and have the other constructors call that routine. This commonly used technique is known as "constructor chaining."
Joined: Mar 22, 2001
Originally posted by Andy Ceponis: Are you maybe referring to 'super'?
Wow, Max that is good. In addition for the this call, sometimes a class has more than one constructor (overloaded). For instance in the Color class there is a constructor to Color(r, g, b) with the red, green and blue parameters passed as integers controlling the amount of each to use. There is ALSO a constructor that calls Color(r, g, b, i) with the final paramter being the intensity or hue of the color requested. Rather that recode the more simple constructor and have duplicate code, the second constructor can just call the first constructor on the first line passing just the first three parameters using "this(r,g,b)", and when that called constructor is complete the additional stuff to set intensity is added. Of course, like Max said, the only way to be sure that all of the groungwork is in place before adding special stuff is to insist that any internal calls are done FIRST.
"JavaRanch, where the deer and the Certified play" - David O'Meara