在java中,this關(guān)鍵字有很多種用法。 在java中,這是一個(gè)引用當(dāng)前對(duì)象的引用變量。
java this關(guān)鍵字的用法如下:
this關(guān)鍵字可用來(lái)引用當(dāng)前類的實(shí)例變量。this關(guān)鍵字可用于調(diào)用當(dāng)前類方法(隱式)。this()可以用來(lái)調(diào)用當(dāng)前類的構(gòu)造函數(shù)。this關(guān)鍵字可作為調(diào)用方法中的參數(shù)傳遞。this關(guān)鍵字可作為參數(shù)在構(gòu)造函數(shù)調(diào)用中傳遞。this關(guān)鍵字可用于從方法返回當(dāng)前類的實(shí)例。建議:如果你是java初學(xué)者,只學(xué)習(xí)
this關(guān)鍵字的前三個(gè)用法就可以了。

this關(guān)鍵字可以用來(lái)引用當(dāng)前類的實(shí)例變量。如果實(shí)例變量和參數(shù)之間存在歧義,則 this 關(guān)鍵字可用于明確地指定類變量以解決歧義問(wèn)題。
了解沒(méi)有 this 關(guān)鍵字的問(wèn)題
下面先來(lái)理解一個(gè)不使用 this 關(guān)鍵字的示例:
class Student {
int rollno;
String name;
float fee;
Student(int rollno, String name, float fee) {
rollno = rollno;
name = name;
fee = fee;
}
void display() {
System.out.println(rollno + " " + name + " " + fee);
}
}
class TestThis1 {
public static void main(String args[]) {
Student s1 = new Student(111, "ankit", 5000f);
Student s2 = new Student(112, "sumit", 6000f);
s1.display();
s2.display();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
0 null 0.0
0 null 0.0
在上面的例子中,參數(shù)(形式參數(shù))和實(shí)例變量(rollno和name)是相同的。 所以要使用this關(guān)鍵字來(lái)區(qū)分局部變量和實(shí)例變量。
使用 this 關(guān)鍵字解決了上面的問(wèn)題
class Student {
int rollno;
String name;
float fee;
Student(int rollno, String name, float fee) {
this.rollno = rollno;
this.name = name;
this.fee = fee;
}
void display() {
System.out.println(rollno + " " + name + " " + fee);
}
}
class TestThis2 {
public static void main(String args[]) {
Student s1 = new Student(111, "ankit", 5000f);
Student s2 = new Student(112, "sumit", 6000f);
s1.display();
s2.display();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
111 ankit 5000
112 sumit 6000
如果局部變量(形式參數(shù))和實(shí)例變量不同,則不需要像下面的程序一樣使用this關(guān)鍵字:
不需要 this 關(guān)鍵字的程序示例
class Student {
int rollno;
String name;
float fee;
Student(int r, String n, float f) {
rollno = r;
name = n;
fee = f;
}
void display() {
System.out.println(rollno + " " + name + " " + fee);
}
}
class TestThis3 {
public static void main(String args[]) {
Student s1 = new Student(111, "ankit", 5000f);
Student s2 = new Student(112, "sumit", 6000f);
s1.display();
s2.display();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
111 ankit 5000
112 sumit 6000
對(duì)變量使用有意義的名稱是一種好的編程習(xí)慣。所以使用相同名稱的實(shí)例變量和參數(shù),并且總是使用
this關(guān)鍵字。
可以使用this關(guān)鍵字調(diào)用當(dāng)前類的方法。如果不使用this關(guān)鍵字,編譯器會(huì)在調(diào)用方法時(shí)自動(dòng)添加此 this 關(guān)鍵字。我們來(lái)看看這個(gè)例子。
執(zhí)行上面代碼輸出結(jié)果如下 -
hello n
hello m
this()構(gòu)造函數(shù)調(diào)用可以用來(lái)調(diào)用當(dāng)前類的構(gòu)造函數(shù)。 它用于重用構(gòu)造函數(shù)。 換句話說(shuō),它用于構(gòu)造函數(shù)鏈接。
從參數(shù)化構(gòu)造函數(shù)調(diào)用默認(rèn)構(gòu)造函數(shù):
class A {
A() {
System.out.println("hello a");
}
A(int x) {
this();
System.out.println(x);
}
}
class TestThis5 {
public static void main(String args[]) {
A a = new A(10);
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
hello a
10
從默認(rèn)構(gòu)造函數(shù)調(diào)用參數(shù)化構(gòu)造函數(shù):
class A {
A() {
this(5);
System.out.println("hello a");
}
A(int x) {
System.out.println(x);
}
}
class TestThis6 {
public static void main(String args[]) {
A a = new A();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
5
hello a
this()構(gòu)造函數(shù)調(diào)用用于從構(gòu)造函數(shù)重用構(gòu)造函數(shù)。 它維護(hù)構(gòu)造函數(shù)之間的鏈,即它用于構(gòu)造函數(shù)鏈接??纯聪旅娼o出的示例,顯示this關(guān)鍵字的實(shí)際使用。
class Student {
int rollno;
String name, course;
float fee;
Student(int rollno, String name, String course) {
this.rollno = rollno;
this.name = name;
this.course = course;
}
Student(int rollno, String name, String course, float fee) {
this(rollno, name, course);// reusing constructor
this.fee = fee;
}
void display() {
System.out.println(rollno + " " + name + " " + course + " " + fee);
}
}
class TestThis7 {
public static void main(String args[]) {
Student s1 = new Student(111, "ankit", "java");
Student s2 = new Student(112, "sumit", "java", 6000f);
s1.display();
s2.display();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
111 ankit java null
112 sumit java 6000
注意:調(diào)用
this()必須是構(gòu)造函數(shù)中的第一個(gè)語(yǔ)句。
下面示例為不把 this() 語(yǔ)句放在第一行,因此編譯不通過(guò)。
class Student {
int rollno;
String name, course;
float fee;
Student(int rollno, String name, String course) {
this.rollno = rollno;
this.name = name;
this.course = course;
}
Student(int rollno, String name, String course, float fee) {
this.fee = fee;
this(rollno, name, course);// C.T.Error
}
void display() {
System.out.println(rollno + " " + name + " " + course + " " + fee);
}
}
class TestThis8 {
public static void main(String args[]) {
Student s1 = new Student(111, "ankit", "java");
Student s2 = new Student(112, "sumit", "java", 6000f);
s1.display();
s2.display();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
Compile Time Error: Call to this must be first statement in constructor
this關(guān)鍵字也可以作為方法中的參數(shù)傳遞。 它主要用于事件處理。 看看下面的一個(gè)例子:
class S2 {
void m(S2 obj) {
System.out.println("method is invoked");
}
void p() {
m(this);
}
public static void main(String args[]) {
S2 s1 = new S2();
s1.p();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
method is invoked
這個(gè)應(yīng)用程序可以作為參數(shù)傳遞:
在事件處理(或)的情況下,必須提供一個(gè)類的引用到另一個(gè)。 它用于在多個(gè)方法中重用一個(gè)對(duì)象。
也可以在構(gòu)造函數(shù)中傳遞this關(guān)鍵字。 如果必須在多個(gè)類中使用一個(gè)對(duì)象,可以使用這種方式。 看看下面的一個(gè)例子:
class B {
A4 obj;
B(A4 obj) {
this.obj = obj;
}
void display() {
System.out.println(obj.data);// using data member of A4 class
}
}
class A4 {
int data = 10;
A4() {
B b = new B(this);
b.display();
}
public static void main(String args[]) {
A4 a = new A4();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
10
可以從方法中 this 關(guān)鍵字作為語(yǔ)句返回。 在這種情況下,方法的返回類型必須是類類型(非原始)。 看看下面的一個(gè)例子:
作為語(yǔ)句返回的語(yǔ)法
return_type method_name(){
return this;
}
從方法中返回為語(yǔ)句的 this 關(guān)鍵字的示例
class A {
A getA() {
return this;
}
void msg() {
System.out.println("Hello java");
}
}
class Test1 {
public static void main(String args[]) {
new A().getA().msg();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
Hello java
驗(yàn)證 this 關(guān)鍵字
現(xiàn)在來(lái)驗(yàn)證 this 關(guān)鍵字引用當(dāng)前類的實(shí)例變量。 在這個(gè)程序中將打印參考變量,這兩個(gè)變量的輸出是相同的。
class A5 {
void m() {
System.out.println(this);// prints same reference ID
}
public static void main(String args[]) {
A5 obj = new A5();
System.out.println(obj);// prints the reference ID
obj.m();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
A5@22b3ea59
A5@22b3ea59