Java Tutorials
Java String indexOf() Method with Substring & Example
What is indexOf() Method in Java? indexOf() Method is used to get index of the first occurrence of a...
this keyword in Java is a reference variable that refers to the current object of a method or a constructor. The main purpose of using this keyword in Java is to remove the confusion between class attributes and parameters that have same names.
Following are various uses of 'this' keyword in Java:
Let's compile and run the code
Our expected output for A and B should be initialized to the values 2 and 3 respectively.
But the value is 0, Why? Let investigate.
In the method Set data, the arguments are declared as a and b, while the instance variables are also named as a and b.
During execution, the compiler is confused. Whether "a" on the left side of the assigned operator is the instance variable or the local variable. Hence, it does not set the value of 'a' when the method set data is called.
The solution is the "this" keyword
Append both 'a' and 'b' with the Java this keyword followed by a dot (.) operator.
During code execution when an object calls the method 'setdata'. The keyword 'this' is replaced by the object handler "obj." (See the image below).
So now the compiler knows,
The variables are initialized correctly, and the expected output is shown.
Suppose you are smart enough to choose different names for your instance variable and methods arguments.
But this time around, you create two objects of the class, each calling the set data method.
How the compiler will determine whether it is supposed to work on instance variable of object 1 or object 2.
Well, the compiler implicitly appends the instance variable with "this" keyword (image below).
Such that when object 1 is calling the set data method, an instance variable is appended by its reference variable.
While object 2 is calling the set data method, an instance variable of object 2 is modified.
This process is taken care by the compiler itself. You don't have to append 'this' keyword explicitly unless there is an exceptional situation as in our example.
Example: To learn the use "this" keyword
Step 1) Copy the following code into a notepad.
class Account{
int a;
int b;
public void setData(int a ,int b){
a = a;
b = b;
}
public void showData(){
System.out.println("Value of A ="+a);
System.out.println("Value of B ="+b);
}
public static void main(String args[]){
Account obj = new Account();
obj.setData(2,3);
obj.showData();
}
} Step 2) Save ,Compile & Run the code.
Step 3) Value of a & b is shown as zero? To correct the error append line # 6 & 7 with "this" keyword.
this.a =a; this.b =b;
Step 4) Save, Compile, and Run the code. This time around, values of a & b are set to 2 & 3 respectively.
What is indexOf() Method in Java? indexOf() Method is used to get index of the first occurrence of a...
Java has had several advanced usage application including working with complex calculations in...
In this JavaScript Unit Testing tutorial, we will learn: What is JavaScript? JavaScript is a...
What is Abstraction in OOP? Abstraction is the concept of object-oriented programming that "shows"...
Why use string "charAt" Method? The charat method returns the character at the definite index. In this...
For-Each Loop is another form of for loop used to traverse the array. for-each loop reduces the...