Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes constructer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructer" Watch "constructer" New topic
Author

constructer

Kashif Rafique
Greenhorn

Joined: Mar 22, 2001
Posts: 2
When we inherit a class why this, keyword, is used on the very 1st line in the constructer? very :confused
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
Are you maybe referring to 'super'?
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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."
Kashif Rafique
Greenhorn

Joined: Mar 22, 2001
Posts: 2
Originally posted by Andy Ceponis:
Are you maybe referring to 'super'?

No. I am referring to only "this" keyword.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructer
 
Similar Threads
SUPER?
Mock Exam
why constructer for abstract class?
Help with constructor
Flowlayout()