Java Interview QuestionsJava_Language_Features

Java This Keyword Explained

Posted On
Posted By admin

In this blog post, I will be explaining the ‘this’ keyword. Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object i.e. the object on which the method was invoked.

Consider the following code snippet:


private int radius;

public  Circle(int radius)

{

this.radius = radius;

}

 

Since both the passed in variables and the instance fields have the same names, this is used to distinguish. So this.radius refers to the current object’s copy of the radius instance field & radius refers to the value passed in.

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment