Java中的this关键字

this关键字
关于带不带static关键字
多数情况下“this.”是可以省略的
this不能使用在带有static的方法当中
例子 this.id = id
this可以用在哪里?

  • this关键字:
    1、this是一个关键字,翻译为:这个。
    2、this是一个引用,this是一个变量,this变量中保存了内存地址指向了自身,this储存在JVM堆内存Java对象内部。
    3、创建100个Java对象,每一个对象都有this,也就是说100个不同的this。

在这里插入图片描述
4、this可以出现在“实例方法”中,this指向当前正在执行这个动作的对象。(this代表当前对象)

  • 关于带不带static关键字

    在这里插入图片描述
  • 多数情况下“this.”是可以省略的。例子:
package com.zyh.customer;

public class Customer {
    String name;
    public Customer(){
    }
    public void shopping(){
        //当张三在购物时,输出张三在购物
        //当李四在购物时,输出李四在购物
        //由于name是一个实例变量,所以这个name访问的时候一定访问的是当前对象的name
        //所以多数情况下“this.”是可以省略的
        //System.out.println(name + "在购物");

        //实例变量必须采用“引用.”的方式访问
        //System.out.println(c1.name + "在购物");
        //System.out.println(c2.name + "在购物");

        //完整写法
        System.out.println(this.name + "在购物");
    }

}
package com.zyh.customer;

public class CustomerTest {
    public static void main(String[] args){
        Customer c1 = new Customer();
        c1.name = "张三";
        c1.shopping();
        Customer c2 = new Customer();
        c2.name = "李四";
        c2.shopping();
    }
}

输出:
在这里插入图片描述

  • this不能使用在带有static的方法当中。
    多数情况下“this.”是可以省略的。
    但是用来区分局部变量和实例变量的时候,“this.”不能省略。
  • this可以用在哪里?
    1、可以使用在实例方法当中,代表当前对象【语法格式:this.】
    2、可以使用在构造方法当中,通过当前的构造方法调用其他的构造方法【语法格式:this(实参);】
    重点:this()这种语法只能出现在构造函数第一行。只能出现一句。
package com.zyh.date;

public class Date {
    //属性私有化
    private int year;
    private int month;
    private int day;

    //构造函数
    //有参
    public Date(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    //无参,默认是2000年1月1日
    public Date() {
/*
        this.year = 2000;
        this.month = 1;
        this.day = 1;
*/
//以上代码可以通过调用另一个构造方法来完成
//但前提是不能创建新的对象,以下代码表示创建了一个全新的对象
//Date d = new Date(2000,1,1);

//需要采用以下的语法来完成构造方法的调用
//这种方式不会创建新的Java对象,同时又可以调用其他的构造方法
this(2000,1,1);
    }

    //getter和setter方法
    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public void print(){
        System.out.println(this.year + "年" + this.month + "月" + this.day + "日");
    }
}

package com.zyh.date;

public class DateTest {
    public static void main(String[] args){
        //创建日期对象1
        Date time1 = new Date();
        time1.print();
        //创建日期对象2
        Date time2 = new Date(2020,3,10);
        time2.print();
    }
}

在这里插入图片描述

  • 带有static的方法,其实既可以采用类名的方式访问,也可以采用引用的方式访问。
    但是即使采用引用的方式去访问,实际上执行的时候和引用指向的对象无关。
    在myeclipse中开发的时候,使用引用的方式访问带有static的方法,程序会出现警告。
    所以带有static的方法还是建议使用“类名.”的方式访问
  • 所以:
    t = null;
    t.some();
    这里不会出现空指针异常
原文地址:https://www.cnblogs.com/yu011/p/12632588.html