Saturday, 24 August 2013

this keyword in Java - Java SE


this is a reference to the current object i.e the object of the class we are using it in. You can refer to any member(method or field) of the current object from within an instance method or a constructor by using the this keyword.

Using this keyword with a Field


You use the this keyword with a field when the field is shadowed by a method or a constructor parameter.

Example 1:


public class Point {



    public int x = 0;

    public int y = 0;

       

    //constructor

    public Point(int a, int b) {

        x = a;

        y = b;

    }

}


Example 2: Modifying above example by shadowing the instance variables.


public class Point {

    public int x = 0;

    public int y = 0;

       

    //constructor

    public Point(int x, int y) {

        this.x = x;

        this.y = y;

    }

}


Notice the code written in bold. In the first example, we use 4 variables(2 instance variables and 2 constructor parameters), all with different names. But in the second example, we have 4 variables but only two names.

Example 2 explanation


The scope of class variables is in the whole class but the scope of the constructor parameters is local to the constructor.
Same name for the constructor parameters and instance variables results in shadowing. This means that when we write x and y in the constructor, it will refer to the local parameters. The local parameters have thus shadowed the instance variables, which means that we cannot access the instance variables directly.

To refer to the instance variables from within the constructor, you have to refer to the members through the instance of the object of the class Point.

Thus, to refer to the Point class field x and y, the constructor must use this.x and this.y, respectively

Using this keyword with a Constructor



You can call another constructor of the same class from within a constructor by using the this keyword. Doing such is called an explicit constructor invocation.

Code Example:


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;

    }

    ...

}


The class Rectangle has 3 constructors. Each constructor initializes some or all of the instance variables. The constructors in the class Rectangle are providing a default value for any member variable whose initial value is not provided by an argument.

Note that, if this keyword is invoking a constructor from another constructor, then it must be the first line in the constructor.


Related Tutorials: 

Exceptions in Java - Detailed Tutorial 

How to find IP Address of a Computer using Java



Get Ebooks delivered to your email id

Comments

Subscribe to our channel

Facebook

Powered by Blogger.

Home | Contact Us | DMCA | Terms of Service | Privacy | Advertise

Maven Scientists