Java This 关键字

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
  1. In the setStudent method, two arguments are declared as number and age.
  2. In the Student class, two instance variables are named as number and age.
  3. 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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值