Java: This keyword

This entry is part 25 of 54 in the series Learn Java

Inside the constructor, as well as in other methods belonging to the object, a special keyword called this can be used.

This keyword is a reference to the current instance of the class.

If, for example, the constructor’s parameters have the same names as the corresponding fields, then the fields could still be accessed by using the this keyword, even though they are shadowed by the parameters.

package javaapplication19;

public class JavaApplication19 {

    static class MyRectangle {
        int x, y;
        public MyRectangle(int x, int y){
            this.x = x;
            this.y = y;
        public int getArea() {
            return x * y;

    public static void main(String[] args) {
        MyRectangle r = new MyRectangle(10, 20);      

        int area = r.getArea();



Series Navigation<< Java: Fields and members of a class
Java: Constructor overloading >>

Leave a comment

Leave a Reply