this Keyword in Java
this is a keyword in java used to denote the current object whose method or is being invoked. this keyword can be used inside the method or constructor of the class. It can be used to refer to any member of the class from within the method or constructor.
public class demo {
int d;
// Constructor with a parameter
public demo(int d) {
this.d = d;
}
// Call the constructor
public static void main(String[] args) {
demo myObj = new demo(500);
System.out.println("Value of d = " + myObj.d);
}
}
this Keyword with a Field
this keyword is used to handling variables in a way that we can have a global variable of a name and we can have a local variable with the same name. a global variable can be accessed by this keyword. With this feature, the local variable will hide the instance variable. This is called variable hiding. Please have a look below the program.
Example of variable hiding
public class demo {
int variable = 5; // instance variable declared
public static void main(String[] args) {
demo ex = new demo();
ex.method(200);
ex.method();
}
void method(int variable) {
variable = 100;
System.out.println("Value of variable :" + variable);
}
void method() {
int variable = 400;
System.out.println("Value of variable :" + variable);
}
}
Here we can see that in the program, the global variable (instance variable) is hiding, and the value of the local variable is getting displayed. To solve this problem we have this keyword which points to an instance variable.
Please have a look below the program.
public class demo {
int variable = 300; // instance variable declared
public static void main(String[] args) {
demo ex = new demo();
ex.method(200);
ex.method();
}
void method(int variable) {
variable = 100;
System.out.println("Value of instance variable :" + this.variable); // display value of instance variable
System.out.println("Value of local variable :" + variable); // display value of local variable
this.variable = variable;
}
void method() {
int variable = 400;
System.out.println("Value of instance variable :" + this.variable); // display value of instance variable
System.out.println("Value of local variable :" + variable); // display value of local variable
}
}
this Keyword with Constructor
this keyword can be used inside the constructor to call another overloaded constructor in the same class. Sometimes we want to initialize parameter constructor on based on the parameters.
public class demo {
int variable = 5; // instance variable declared
public demo() {
this(0);
System.out.println("no-arg constructor called");
}
public demo(int variable) {
this.variable = variable;
System.out.println("int parameterized constructor called");
}
public static void main(String[] args) {
demo d = new demo();
System.out.println(d.variable);
}
}
Example of this keyword with Method
class demo {
public static void main(String[] args) {
demo obj = new demo();
obj.methodTwo();
}
void methodOne(){
System.out.println("Inside Method One");
}
void methodTwo(){
System.out.println("Inside Method Two");
this.methodOne();// same as calling methodOne()
}
}
Example of this keyword as a Method parameter
public class demo {
public static void main(String[] args) {
test obj = new test();
obj.d = 20;
obj.method();
}
}
class test extends demo {
int d;
void method() {
method1(this);
}
void method1(test t) {
System.out.println(t.d);
}
}