This Keyword Part 1 Tutorial

The this keyword contains a reference to the current object from 'inside' of the object. You can access any member of the current object using this from within a method or constructor. This tutorial will explain how to use the this keyword to access instance variables. It is very common to name the constructor or method parameter variable the same name as the instance variable. I will modify my Box class from my Constructor Overloading Tutorial to demonstrate how to use this.



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 ThisKeyword
C:\Java>cd ThisKeyword
C:\Java\ThisKeyword>Notepad ThisKeyword.java

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


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

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

        Box d = new Box(7, 5, 6, "inches");
        System.out.println("Inside of the main method d = " + d);
        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(); 
        System.out.println("Inside of the no argument constructor this = " + this);
    }

    // First Constructor created
    Box(int length, int height, int width) {
        System.out.println("Inside of the three parameter constructor this = " + this);
        this.length = length;
        this.height = height;
        this.width = width;
    } 
    
    // New Constructor
    Box(int length, int height, int width, String unitOfMeasurement ) {
        System.out.println("Inside of the four parameter constructor this = " + this);
        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 ThisKeyword.java and press Enter.
Now type in java ThisKeyword and press Enter.


C:\Java\ThisKeyword>javac ThisKeyword.java
C:\Java\ThisKeyword>java ThisKeyword
Inside of the no argument constructor this = Box@659e0bfd
Inside of the main method b = Box@659e0bfd
The volume of box b is: 100

Inside of the three parameter constructor this = Box@62a139a55
Inside of the main method b = Box@62a139a55
The volume of box c is: 96

Inside of the four parameter constructor this = Box@15db9742
Inside of the main method b = Box@15db9742
The volume of box d is: 210 cubic inches


Final thoughts

There are a couple of schools of thought on whether to name your parameter variables the same name as your instance variables or to name them differently. The same argument can apply to both ways - to eliminate confusion. If you name the parameter variable different, but similar to the instance variable, there is no need for the this keyword and that will eliminate confusion. However, if you name the parameter variable and the instance variable the same name, but include the this keyword for the instance variable, then there is now question about which parameter variable will change which instance variable value. Using the this version is a slightly more advanced technique, but the choice is up to you.


Tutorials