This Keyword Part 2 Tutorial

In part 1, we learned that the this keyword contains a reference to the current object from 'inside' of the object. We can use this to invoke other overloaded constructors from within the code block of a constructor. There is one important rule to remember, the this statement used to invoke another constructor must be the first statement of the constructor's code block. In this tutorial I want to make my unitOfMeasurement instance variable default to "inches". I will use this to do just that.

There is one more rule that is a little more advanced to explain at this point. You won't need to understand the purpose of the super keyword just yet, but a constructor can only have a call either to the super(); statement or the this(); statement; both cannot be called within the same code block.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ThisKeywordTwo
C:\Java>cd ThisKeywordTwo
C:\Java\ThisKeywordTwo>Notepad ThisKeywordTwo.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class ThisKeywordTwo {
    public static void main(String args[]) {
	
        Box b = new Box(); 
        b.setLength(10);
        b.setHeight(2);		
        b.setWidth(5);
        System.out.println("The volume of box b is: " + b.calculateVolume() + " cubic " + b.getUnitOfMeasurement());
        System.out.println();

        Box c = new Box(4, 8, 3);
        System.out.println("The volume of box c is: " + c.calculateVolume() + " cubic " + c.getUnitOfMeasurement());
        System.out.println();

        Box d = new Box(18, 15, 21, "centimeters");
        System.out.println("The volume of box d is: " + d.calculateVolume() + " cubic " + d.getUnitOfMeasurement());
        System.out.println();
    }
}

class Box {
    private int length = 0; 
    private int height = 0; 
    private int width = 0;
    private String unitOfMeasurement; 

    // Default constructor - Don't forget to always include this.
    Box() {
        //super();  remove this
        this(0, 0, 0, "inches");
    }

    // First Constructor created
    Box(int length, int height, int width) {
        this(length, height, width, "inches");
        //this.length = length; remove these now
        //this.height = height; remove these now
        //this.width = width; remove these now
    } 
    
    // New Constructor
    Box(int length, int height, int width, String unitOfMeasurement ) {
        this.length = length;
        this.height = height;
        this.width = width;
        this.unitOfMeasurement = unitOfMeasurement;
    }      

    void setLength (int length) {
        this.length = length;
    }
    int getLength () {
        return length;
    }

    void setHeight (int height) {
        this.height = height;
    }
    int getHeight () {
        return height;
    }

    void setWidth (int width) {
        this.width = width;
    }
    int getWidth () {
        return width;
    }

    void setUnitOfMeasurement (String unitOfMeasurement) {
        this.unitOfMeasurement = unitOfMeasurement;
    }
    String getUnitOfMeasurement () {
        return unitOfMeasurement;
    }

    // create a method to get the volume of the box
    int calculateVolume() {
       return (length * height * width);	
    }
}

Now switch back to the command prompt (CMD) and type in javac ThisKeywordTwo.java and press Enter.
Now type in java ThisKeywordTwo and press Enter.


C:\Java\ThisKeywordTwo>javac ThisKeywordTwo.java
C:\Java\ThisKeywordTwo>java ThisKeywordTwo
The volume of box b is: 100 cubic inches

The volume of box c is: 96 cubic inches

The volume of box d is: 5670 cubic centimeters


Final thoughts

The ability to overload constructors provides a really cool tool to ensure that your code will always have scalability while at the same time supporting previous versions. Using this to invoke new constructors provides a smooth way to add new members and functionality to your classes.


Tutorials