This Keyword
This is a reference variable that refers to the current object.
Usage Of Java this Keyword.
This is a reference variable that refers to the current object.
Usage Of Java this Keyword.
- this keyword can be used to refer current class instance variable.
- this() can be used to invoke current class constructor.
- this keyword can be used to invoke current class method (implicitly)
- this can be passed as an argument in the method call.
- this can be passed as argument in the constructor call.
- this keyword can also be used to return the current class instance.
/*Concept Of This keyword Example In Java.*/
● Open notepad and add the code.
Output:-
//programming789.blogspot.com
class KeyTest
{
int id;
String name;
KeyTest(int id,String name)
{
this.id = id;
this.id = id;
this.name = name;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
public static void main(String args[])
{
KeyTest s1 = new KeyTest(142,"Bob");
KeyTest s2 = new KeyTest(452,"John");
s1.display();
s2.display();
}
}
● Open notepad and add the code.
● Save the file as KeyTest.java.
Output:-
>java KeyTest
142 Bob
452 John