The meaning and role of the Java keyword this

Source: Internet
Author: User

The This keyword has three applications: (1) This invokes the attribute in this class, that is, the member variable in the class, (2) This invokes the other methods in this class, and (3) this invokes the other constructor methods in this class, and is placed on the first line of the constructor method when called. Public Class Student {string name;//define a member variable name private void SetName (String name) {//define a parameter (local variable) name This.name=name ; Pass the value of the local variable to the member variable}} Apply one: Reference member variable as in the preceding code, there is a member variable name, and there is a formal parameter in the method, the name is name, and then in the method pass the value of the form parameter name to the member variable name, Although we can see the meaning of this code, but as a Java compiler how is it judged? Is it true that the value of the formal parameter name is passed to the member variable name, or the value of the member variable name is passed to the formal parameter name? That is, if the two variable names are the same, So how does Java determine which variable to use? This keyword will play a role. This keyword represents a member variable or method in an object. That is, if you precede a variable with the This keyword, it refers to the member variable or method of the object, not the formal parameter or local variable of the member method. In this code above, THIS.name represents the member variable in the object, also called the object's property, and the following name is the formal parameter of the method, and the code this.name=name is to pass the value of the formal parameter to the member variable. This is exactly what the above code means. In general, referencing member variables or member methods in the Java language is the object name. A member variable or an object name. The form of a member method. Some programmers, however, prefer to use this as a member variable, even when they do not have the same variable, which is mainly from the convenience of code reading. As soon as you see this keyword, you know that the variable you are referencing is either a member variable or a member method, not a local variable. This virtually improves the readability of the code. But then again, this is the simplest application of the This keyword in the Java language. From this application, we can see that the This keyword represents the name of the object. In fact, if it is a local variable, it is the same reason. As in the above code, name is not a formal parameter, but a local variable. Java will also encounter the same confusion, that is, the variable name name is a local variable or a formal parameter? nameWhat does =name mean? According to the scope of the local variable, within the method, if the local variable has the same name as the member variable, then the local variable will prevail. However, in Name=name this assignment statement, it is obviously not appropriate to assign the value of the local variable to itself. According to the meaning of the code, the original meaning should be to assign a local variable to the member variable. In order to express this meaning more clearly, it is best to use the following writing format This.name=name. The This keyword here means the object name student, for which this.name represents Student.name.  Application two: Constructing method of calling class public class Student {//defines a class whose name is Student. Public Student () {///define a method whose name is the same as the class so it is constructed by this ("hello!"), and the public Student (String name) {//Defines a constructor method with formal arguments}} The This keyword in addition to You can also call a constructor method outside of the calling member variable. In a Java class, its methods can be divided into the member method and the construction method two kinds. A constructor method is a method that has the same name as a class, and a constructor method must exist in the Java class. If the constructor is not shown in the code, then the compiler will automatically add a constructor with no formal parameters when compiling. There are many different ways of constructing this method with ordinary member methods. If the constructor method has no return value, and the Void keyword is not used to indicate that the constructor does not return a value. A normal method can have a return value, or it can have no return value, and programmers can define it according to their own needs. However, if the normal method does not return a value, then be sure to use the Void keyword when the method definition is described. Second, the name of the construction method has strict requirements, that is, the name of the class must be the same. In other words, the Java compiler finds a method that is the same as the name of the class to treat it as a construction method. For common methods, the requirement cannot be the same as the name of the class, and multiple member methods cannot use the same name. There can be more than one construction method in a class that uses the same name, except for the formal parameter. The Java language determines the invocation of the constructor by the different form parameters. In the above code, two construction methods are defined, one with parameters and the other without parameters. The constructor method does not have a return value, but because of the particularity of the construction method, it is not necessary to take the void keyword when constructing the method definition to illustrate the problem. In the first constructed method without parameters, this ("hello!") is used This code, what does this code mean?In the constructor method, the This keyword represents the constructor method in the calling class. If there is more than one constructor in a class, because its name is the same and is consistent with the name of the class, what is the constructor called? In fact, this is the same as using other methods to refer to the constructor method, which is called by the formal parameter. As in the example above, the This keyword is followed by a parameter, which means that it refers to a constructor with parameters. If there are now three constructor methods, with no parameters, with one parameter, with two parameters. Then the Java compiler will determine which constructor to call based on the number of arguments passed. As you can see from the example above, the This keyword can be used not only to reference member variables, but also to reference construction methods. However, if you want to invoke the constructor method in this way, there is a syntax limitation. In general, using the This keyword to invoke the constructor method, only the first sentence in the parameterless constructor method uses this to invoke the constructor with the parameter. Otherwise, when translating, there will be error messages. This is different from referencing member variables. If you refer to a member variable, the This keyword is not limited in position. If you are unfamiliar with this limitation, it is good to use the traditional method of constructor invocation. It's a hassle, but at least it doesn't go wrong. Apply three: Return the value of an object the This keyword, in addition to referencing variables or member methods, has a significant effect of returning a reference to a class. As in code, you can use return this to return a reference to a class. At this point, the This keyword represents the name of the class. If the code is in the student class above, then the code means return student. It can be seen that the this keyword, in addition to referencing variables or member methods, also serves as the return value of the class, which is the most noticeable part of this keyword.

The meaning and effect of the keyword this of Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.