This key word in Java
A lot of people must know how to use “this” in java programming, but do they really know what exact This is and why does it exist ?
1 - Why we need to use “This” in java?
class Student{
int number;
int age;// two instance variables
public void setStudent(int number, int age){// two arguments
number = number;
age = age;
}
public void display(){
System.out.println(number);
System.out.println(age);
}
public void main(String[] args){
Student s = new Student(123, 18);
s.display();
}
}
result: 0
0
- In the setStudent method, two arguments are declared as number and age.
- In the Student class, two instance variables are named as number and age.
- When the code are executed, the compiler gets confused. whether the left side of operator is instance variable or local variable. So, it will not set the data to number and age when the setStudent method is called.
So we need to use this key word to remove confusion between instance variables and local variables. (using this to refer to instance variables of a method or constructor)
2 - How to use this in java ?
1) using this to refer instance variable of current class.
eg 1.
class Student{
int number = 100;// instance variable
int age = 100;
public void show(){
System.out.println(this.number);// or number
System.out.println(this.age);// or age
}
public static void main(String[] args){
Student s = new Student();
s.show();
}
}
result: 100
100
eg 2.
class Student{
int number = 100;// instance variable
int age = 100;
public void setStudent(int number, int age){
this.number = number;// left this.number is instance variable in student class, right number is the argument that was given in main class
this.age = age;
}
public void show(){
System.out.println(this.number);// or number
System.out.println(this.age);// or age
}
public static void main(String[] args){
Student s = new Student(1, 1);
s.show();
}
}
result: 1
1
2)using this to pass method as an argument
class Student{
int number = 100;// instance variable
public void setStudent(int number){
this.number = number;
this.display();
}
public void show(){
System.out.println(this.number);
}
public void display(){
System.out.println("invoking display method");
}
public static void main(String[] args){
Student s = new Student(1);
s.show();
}
}
result: invoking display method
1
3) using "this()"invoking current constructor
class Student{
student(){
System.out.println("invoking first constructor");
}
student(int number){
this();
Sytem.out.println(number);
}
public static void main(String[] args){
Student s = new Student(100);
}
}
result: invoking first constructor
100
4) using this to return the current class instance (passing the class instance)
class Student{
void print(){
System.out.println(this);
}
public static void main(String[] args){
Student s = new Student();
System.out.println(s);
s.print();
}
}
result: Student@1540e19d (class instance ID)
Student@1540e19d
class Person{
void mom(Person p){
System.out.println("invoking mom method");
}
void dad(){
mom(this);
}
public static void main(String[] args){
Person p = new Person();
p.dad();
}
}
result: invoking mom method