Java – why use the “this” keyword in constructors and setters?

Constructor is used to initialize a value and assign it to class variables when creating class instances, right?

public class Joke{
   private String jokeSetup;
   private String jokePunchLine;

   public Joke(String jokeSetup,String jokePunchLine){
       this.jokeSetup=jokeSetup;
       this.jokePunchLine=jokePunchLine;
   }
}

Consider the following:

public Joke(String jokeSetup,String jokePunchLine)

Did you create another variable with the same name?

If so, why are they assigned to the previous jokesetup and jokepunchline values?

PS: this code was not created by me It is shown in my video on learning Java

Solution

The purpose of the constructor is to initialize the object just created, such as filling its instance field (also known as instance variable) This is used in the constructor to reference an instance of constructor initialization

In the sample constructor, you have parameters and instance fields The constructor is taking the values of the parameters and assigning them to the instance field:

public Joke(String jokeSetup,String jokePunchLine)
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---- Declares parameters this
//                                                      constructor accepts when
//                                                      called
{
//                 vvvvvvvvv------------ parameter
    this.jokeSetup=jokeSetup;
//  ^^^^^^^^^^^^^^---------------------- instance field

//                     vvvvvvvvvvvvv---- parameter
    this.jokePunchLine=jokePunchLine;
//  ^^^^^^^^^^^^^^^^^^------------------ instance field
}

Constructors can initialize instance fields with constant values, or indirectly use parameter values (for example, find something), etc It is not always a direct one - to - one assignment, as in your example

In your example, the parameter has the same name as the instance field, but this is not required For example, this constructor is exactly the same as your constructor:

public Joke(String theJokeSetup,String theJokePunchLine)
//                 ^---------------------^---------- Note the name changes
{
//                   vvvvvvvvvvvv------------ parameter
    this.jokeSetup = theJokeSetup;
//  ^^^^^^^^^^^^^^--------------------------- instance field

//                       vvvvvvvvvvvvvvvv---- parameter
    this.jokePunchLine = theJokePunchLine;
//  ^^^^^^^^^^^^^^^^^^----------------------- instance field
}

Java lets you not use this When partially referencing instance fields, you only need to use the field name alone (for example, jokesetup instead of this. Jokesetup) However, you cannot perform this operation in the constructor unless you rename the parameters, because they have the same name as the instance field, so the jokesetup in the constructor is a parameter, not a field When a similar conflict occurs, the most local identifier takes precedence (in the constructor, the parameter is the most local)

When there is no conflict, whether you use it or not is a style problem Partial or no I always use it. I find it clearer For example, this is another version of the constructor, which is exactly the same as the original version:

public Joke(String theJokeSetup,String theJokePunchLine)
//                 ^---------------------^---------- Note the name changes
{
//              vvvvvvvvvvvv------------ parameter
    jokeSetup = theJokeSetup;
//  ^^^^^^^^^--------------------------- instance field

//                  vvvvvvvvvvvvvvvv---- parameter
    jokePunchLine = theJokePunchLine;
//  ^^^^^^^^^^^^^----------------------- instance field
}

I mention this because, when there is no conflict, it is a matter of style, and you will see that this style is sometimes used

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>