Guide to the this Java Keyword – 这个Java关键词的指南

最后修改: 2018年 6月 2日

中文/混合/英文(键盘快捷键:t)

 

1. Introduction

1.介绍

In this tutorial, we’ll take a look at the this Java keyword.

在本教程中,我们将看一下thisJava关键字。

In Java, this keyword is a reference to the current object whose method is being called.

在Java中,this关键字是对其方法被调用的当前对象的引用

Let’s explore how and when we can use the keyword.

让我们探讨一下如何以及何时可以使用这个关键词。

2. Disambiguating Field Shadowing

2.消除场域阴影

The keyword is useful for disambiguating instance variables from local parameters. The most common reason is when we have constructor parameters with the same name as instance fields:

该关键字对于区分实例变量和局部参数很有用。最常见的原因是当我们有与实例字段同名的构造器参数时。

public class KeywordTest {

    private String name;
    private int age;
    
    public KeywordTest(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

As we can see here, we’re using this with the name and age instance fields – to distinguish them from parameters.

正如我们在这里看到的,我们在this中使用了nameage实例字段–以区分它们与参数。

Another usage is to use this with the parameter hiding or shadowing in the local scope. An example of use can be found in the Variable and Method Hiding article.

另一种用法是在本地范围内使用this与参数隐藏或阴影。在变量和方法隐藏文章中可以找到一个使用实例。

3. Referencing Constructors of the Same Class

3.引用同一类别的构造函数

From a constructor, we can use this() to call a different constructor of the same class. Here, we use this() for the constructor chaining to reduce the code usage.

从一个构造函数中,我们可以使用this()来调用同一类别的不同构造函数。在这里,我们使用this()进行构造函数链,以减少代码的使用。

The most common use case is to call a default constructor from the parameterized constructor:

最常见的使用情况是,从参数化构造函数中调用一个默认的构造函数。

public KeywordTest(String name, int age) {
    this();
    
    // the rest of the code
}

Or, we can call the parameterized constructor from the no argument constructor and pass some arguments:

或者,我们可以从无参数构造函数中调用参数化构造函数并传递一些参数。

public KeywordTest() {
    this("John", 27);
}

Note, that this() should be the first statement in the constructor, otherwise the compilation error will occur.

注意,this()应该是构造函数的第一个语句,否则会发生编译错误。

4. Passing this as a Parameter

4.将this作为一个参数传递

Here we have printInstance() method, where the this Keyword argument is defined:

这里我们有printInstance()方法,其中this Keyword参数被定义。

public KeywordTest() {
    printInstance(this);
}

public void printInstance(KeywordTest thisKeyword) {
    System.out.println(thisKeyword);
}

Inside the constructor, we invoke printInstance() method. With this, we pass a reference to the current instance.

在构造函数中,我们调用了printInstance()方法。通过this,我们传递一个对当前实例的引用。

5. Returning this

5 返回这个

We can also use this keyword to return the current class instance from the method.

我们也可以使用this关键字来从方法中返回当前的类实例

To not duplicate the code, here’s a full practical example of how it’s implemented in the builder design pattern.

为了不重复代码,这里有一个完整的实际例子,说明它是如何在builder设计模式中实现的。

6. The this Keyword Within the Inner Class

6.内类中的this关键字

We also use this to access the outer class instance from within the inner class:

我们还使用this来从内层类中访问外层类的实例。

public class KeywordTest {

    private String name;

    class ThisInnerClass {

        boolean isInnerClass = true;

        public ThisInnerClass() {
            KeywordTest thisKeyword = KeywordTest.this;
            String outerString = KeywordTest.this.name;
        }
    }
}

Here, inside the constructor, we can get a reference to the KeywordTest instance with the KeywordTest.this call. We can go even deeper and access the instance variables like KeywordTest.this.name field.

在这里,在构造函数里面,我们可以通过KeywordTest.this调用获得对KeywordTest实例的引用。我们可以更深入地访问实例变量,比如KeywordTest.this.name字段。

7. Conclusion

7.结论

In this article, we explored the this keyword in Java.

在这篇文章中,我们探讨了Java中的this关键字。

As usual, the complete code is available over on Github.

像往常一样,完整的代码可以在Github上找到