Java

Example of Using this Keyword in Java

This article describes an Example of Using this Keyword in Java. Basically, this keyword in Java represents the reference to the current object. Whenever we need to represent the current object or any member of the current object, we can use this keyword. Moreover, we can use it to distinguish between the instance variables and method parameters in case both have the same name.

Important Points Regarding this Keyword

While we can use this keyword to refer to the instance variables of the current object, there are more uses for it. For instance, we can also pass it as the parameter to a method. Also, we can use this keyword to return the current object. Furthermore, we can also call a constructor using this keyword.

Cope Example of Using this Keyword in Java

The following examples illustrate the use of this keyword in several different ways. So, the first example shows that within the parameterized constructor, the parameters and instance variables have the same name. Therefore, we can use this keyword with instance variables to distinguish them from the parameters. Further, the following program also demonstrates how we can call another constructor using this keyword. Accordingly, the parameterized constructor taking one argument is calling another constructor having two arguments using the expression this(x, x+10).

Another use of this keyword in the following program is to return the current instance from a method. In fact, the method increment() updates the instance variables of the current object and then returns the current object using the statement return this.

public class Main
{
	public static void main(String[] args) {
		System.out.println("Creating First Object....");
		MyClass ob1=new MyClass();
		ob1.show();
		ob1=ob1.increment(8, 12);
		ob1.show();
		System.out.println("Creating Second Object....");
		MyClass ob2=new MyClass(673, 745);
		ob2.show();
		System.out.println("Creating Third Object....");
		MyClass ob3=new MyClass(73);
		ob3.show();
		
	}
}
class MyClass
{
    int x, y;
    public MyClass()
    {
	System.out.println("Default Constructor....");
        x=1;
        y=2;
    }
    public MyClass(int x, int y)
    {
	System.out.println("Parameterized Constructor with two parameters....");
        this.x=x;
        this.y=y;
    }
    public MyClass(int x)
    {
       this(x, x+10);
       System.out.println("Parameterized Constructor with one parameter....");
    }
    public MyClass increment(int x, int y)
    {
        this.x+=x;
        this.y+=y;
        return this;
    }
   
    public void show()
    {
        System.out.println("x = "+x+", y = "+y);
    }
}

Output

The Output Produced by the Example of Using this Keyword in Java
The Output Produced by the Example of Using this Keyword in Java

The following example shows one more in use of this keyword. Accordingly, the current object is passed as a parameter in a call to the method power().

public class Main
{
	public static void main(String[] args) {
		MyClass ob=new MyClass(3, 5);
		ob.show();
	}
}
class MyClass{
    int x, y, pw;
    public MyClass(int x, int y)
    {
        this.x=x;
        this.y=y;
        power(this);
    }
    public void power(MyClass m)
    {
        m.pw=1;
        for(int i=1;i<=m.y;i++)
        {
            m.pw*=m.x;
        }
    }
    public void show()
    {
        System.out.println(x +" raised to the power "+y+" = "+pw);
    }
}

Output

Example of Passing this as a Parameter to a Method
Example of Passing this as a Parameter to a Method

To summarize, we can use this keyword when we need to refer to the current instance. Furthermore, we can use this keyword within an instance method, or a constructor. Moreover, inside a constructor, the call to another constructor must be the first statement. Also, we can use in return statement as well as a parameter to a method.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *