View RSS Feed

Java Object

Using this with a Constructor

Rate this Entry
by , 04-26-2012 at 04:41 PM (408 Views)
Within constructor, this keyword could also be used to call the other constructor present in same class. This is known as explicit constructor invocation. Other Rectangle class present here along with varying implementations from that which is present in Objects section.
Java Code:
public class Rectangle {
    private int x, y;
    private int width, height;
    public Rectangle() {
        this(0, 0, 0, 0);
    public Rectangle(int width, int height) {
        this(0, 0, width, height);
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
Set of constructors is present in this class. Few or all member variables of the rectangle are initialized by every constructor. Default value is provided by constructors for member variables, for which initial value is not been given by an argument. For instance, four-argument constructor is called by the no-argument constructor with four zero values. Four argument constructors are called by the two argument constructor with two zero values. Just like it has been done before the compiler would be determining that which constructor shall be called and it is dependent at the type and number of the arguments.

Submit "Using this with a Constructor" to Facebook Submit "Using this with a Constructor" to Digg Submit "Using this with a Constructor" to Submit "Using this with a Constructor" to StumbleUpon Submit "Using this with a Constructor" to Google