File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructer" Watch "constructer" New topic


Kashif Rafique

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

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"

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
It is sorta covered in the JavaRanch Style Guide.
subject: constructer
It's not a secret anymore!