在学了this关键字的作用原理以及使用方法之后,用以下例子来验证一下在java方法调用过程中this关键字在背后所起的作用。
public class ThisFinalTest04 {
//没有static的变量
int i = 10;
//带有static的变量
static int j = 20;
//带有static的方法
public static void doSome(){
System.out.println("do some!");
}
//不带static的方法
public void doOther(){
System.out.println("do other!");
}
//带有static的方法
public static void method1(){
System.out.println("----------------调用doSome方法——在静态方法中调用另一个静态方法---------------");
System.out.print("完整方式调用——类名.方法名(): ");
ThisFinalTest04.doSome();
System.out.print("省略方式调用——方法名(): ");
doSome();
System.out.println("----------------调用doOther方法——在静态方法中调用实例方法-------------------");
System.out.print("完整方式调用——引用.方法名(): ");
ThisFinalTest04 t1 = new ThisFinalTest04();
t1.doOther();
System.out.println("省略方式调用——方法名(): //编译错误:无法从静态上下文中引用非静态 方法 doOther()");
// doOther(); //编译错误:无法从静态上下文中引用非静态 方法 doOther()
System.out.println("---------------------访问i——在静态方法中访问实例变量---------------------");
System.out.print("完整方式访问——先创建对象再通过 引用.实例变量名: ");
ThisFinalTest04 t2 = new ThisFinalTest04();
System.out.println(t2.i);
System.out.println("省略方式访问——实例将变量名: 编译错误:无法从静态上下文中引用非静态 变量 i");
// System.out.println(i); //编译错误:无法从静态上下文中引用非静态 变量 i
System.out.println("---------------------访问j——在静态方法中访问静态变量---------------------");
System.out.print("完整方式访问——类名.静态变量名: ");
System.out.println(ThisFinalTest04.j);
System.out.print("省略方式访问——静态变量名: ");
System.out.println(j);
System.out.println("===============================================================");
}
//不带static的方法——实例方法
public void method2(){
System.out.println("----------------------调用doSome方法——在实例方法中调用静态方法----------------------");
System.out.print("完整方式调用——类名.静态方法名(): ");
ThisFinalTest04.doSome();
System.out.print("省略方式调用——静态方法名(): ");
doSome();
System.out.println("------------------调用doOther方法——在实例方法中调用另一个实例方法-------------------");
System.out.print("完整方式调用——引用.实例方法名(): ");
this.doOther(); //method2方法在执行的时候就已经有了一个对象了,而该对象中已经有了该类中所有的实例方法
System.out.print("省略方式调用——实例方法名(): ");
doOther();
System.out.println("----------------------------访问i——在实例方法中访问实例变量i------------------------");
System.out.print("完整方式访问——引用.实例变量名: ");
System.out.println(this.i);
System.out.print("省略方式访问——实例变量名: ");
System.out.println(i);
System.out.println("----------------------------访问j——在实例方法中访问静态变量j------------------------");
System.out.print("完整方式访问——引用.静态变量名: ");
System.out.println(this.j);
System.out.print("省略方式访问——静态变量名: ");
System.out.println(j);
}
public static void main(String[] args) {
method1();
ThisFinalTest04 t = new ThisFinalTest04();
t.method2();
}
}
运行结果:
----------------调用doSome方法——在静态方法中调用另一个静态方法---------------
完整方式调用——类名.方法名(): do some!
省略方式调用——方法名(): do some!
----------------调用doOther方法——在静态方法中调用实例方法-------------------
完整方式调用——引用.方法名(): do other!
省略方式调用——方法名(): //编译错误:无法从静态上下文中引用非静态 方法 doOther()
---------------------访问i——在静态方法中访问实例变量---------------------
完整方式访问——先创建对象再通过 引用.实例变量名: 10
省略方式访问——实例将变量名: 编译错误:无法从静态上下文中引用非静态 变量 i
---------------------访问j——在静态方法中访问静态变量---------------------
完整方式访问——类名.静态变量名: 20
省略方式访问——静态变量名: 20
===============================================================
----------------------调用doSome方法——在实例方法中调用静态方法----------------------
完整方式调用——类名.静态方法名(): do some!
省略方式调用——静态方法名(): do some!
------------------调用doOther方法——在实例方法中调用另一个实例方法-------------------
完整方式调用——引用.实例方法名(): do other!
省略方式调用——实例方法名(): do other!
----------------------------访问i——在实例方法中访问实例变量i------------------------
完整方式访问——引用.实例变量名: 10
省略方式访问——实例变量名: 10
----------------------------访问j——在实例方法中访问静态变量j------------------------
完整方式访问——引用.静态变量名: 20
省略方式访问——静态变量名: 20
Process finished with exit code 0
将以上案例中的结论总结为如下的思维导图: