Java language keyword usage of this comprehensive summary

Learn Java programming, no matter who, when learning object-oriented will encounter a keyword this section: this. Many beginners feel this keyword will not thoroughly understood, do not understand this mysterious "this" What does it mean in the end. According to the official formal explanation, meaning this keyword is interpreted as a "reference point to the current object." This explanation is very accurate and very refined, but it is too academic, and cause a lot of beginners a little not read, much less in-depth understanding of its meaning. This article will help novice Java junior partner in the form of vernacular to understand the significance of this in-depth keyword, and sort out its various uses.

In fact, we just put this understanding into "this object's own ..." on it. See this explanation, many people may be more ignorant, what is "this object's own ..."? Do not worry, let us first look at a piece of code.

Java language keyword usage of this comprehensive summary
In this code, we define a representation of "people" class Person, the Person class, there are three attributes name, age and height, respectively, to indicate full name, age and height. We define a class constructor in which, the construction method of the three attributes of the initialization operation. Finally introduce defines a method, this method can print the name and age of a Person object. Now, we create an object in the main method among a Person class, and call introduce the method of this object, as follows:

Java language keyword usage of this comprehensive summary
Pleasant run the program, run out of the result is this:

Java language keyword usage of this comprehensive summary
By running the program, we can see the results, when you create an object, the object's properties have been given the correct initial value. The program itself is very simple, anyone can understand, but we note that we define the constructor when the parameters indicating name, age and height were named as: n, a and h, this naming readable of little difference, in order to improve readability, we constructor parameter names modified to name, age, and height, as shown below:

Java language keyword usage of this comprehensive summary
After modification, the main method to run again, the results obtained become like this:

Java language keyword usage of this comprehensive summary
Why is this a result of a problem running it? Because, after modifying the constructor when we call the constructor to create the object, to three parameters passed by constructor of "John Doe", 20 and 178.5 final and not subject to the assignment of three properties. Well, since no parameter value is assigned to the object's properties, they are gone it? After modifying the code, the same name attribute class constructor parameters defined in accordance with "the same name, the higher priority the local variable" principle, in the process of constructing method performed in the virtual machine assigned parameter value "parameter itself "instead of assigning attributes to objects! Specifically, what we value parameter passed to the constructor's name is "Joe Smith" and the "San" in the constructor executes, when running the "name = name;" when this statement, and not the "Joe Smith" is assigned to name attributes of the object, but again the name assigned to the parameter itself. Because "Joe Smith" ultimately not been assigned to the name attribute object, which leads to introduce the method to print out the name attribute is null. Of course, age and height of these two parameters is assigned the same effect.
In order to allow virtual machines to understand what we expect is: "Joe Smith" the string assigned to the name attribute of an object, rather than "once again" assign it to argument constructor, you need to assign construction methods statement is modified as follows:
Java language keyword usage of this comprehensive summary
we look at this time, we are in the constructor, to the "=" in front of the left attributes added this keyword, after modification, re-run the main method, was restored to normal operation effect. Well, now we have to explore what, after the addition of this keyword, why the program can be "back to normal." We have just said, "this" can be interpreted as "this object's own ...", according to this logic, "this.name" can be interpreted as "this object's own name attribute," so in the implementation of "this .name = name; "this statement when the virtual machine will be the value of the parameter name" John Doe "name attribute assigned to the object. That in this assignment, the "=" to the left "this.name" represents the name property of the object, and "=" parameter name represents the name of the right method.
Talking about this, some small partners might ask: "this.name" Why not be interpreted as "this object's own name parameter" mean? Because the "parameters" of this concept is in terms of a method, which is equivalent to "local variable" a method, but this "local" than the real local variables defined in the method in terms of a bit special, it capable of receiving the value transmitted from the calling process over. So when we say "parameter" concept of time, is relative to a "method" rather than an "object" is concerned, so there would be no "parameter of an object," the statement. Therefore, "this.name" can only be identified based virtual machine object's own name "property" and will never be treated as name "parameter."

Through this example, I hope you can understand the meaning of this keyword. In the program, "this. Attribute name" that appears to be the most common form of this keyword usage, we first use is for everyone summary of this keyword. Since "this. Attribute name" may be interpreted as "XX of this object's own property," then there will be any "this. Method name" this use of it? Of course, there are such usage, when the program appears in "this. Method name" such an approach, it can be interpreted as a call to "XX of this object's own methods." Consider the following code:
Java language keyword usage of this comprehensive summary
This time, we have to Person class adds a "hello" method is called greet. Introduce the method in which you can call this method "this. Method name" approach, it indicates that the call is "this object's own greet" method. This is the second use of this keyword. Of course, there has been no other objects introduce method, so the method name in front of this key also can be omitted.

Since this keyword indicates that this object itself, so the code can be directly used to represent this object itself. For example, the class which defines a Ojbect equals () method, which is used to compare itself with other objects are objects are not equal, this can be used to make direct comparisons with other objects.

Java language keyword usage of this comprehensive summary
The code above is equals Object class () method of the source code, are interested can go and see a small partner. In the code directly with this keyword represents the object itself, and another object obj compared. (What about this than the == operator in the end, we can see another one of my blog post " Do you really mastered the language of Java" == "operator? I think not! ")

In fact, this keyword there is another very important use, that is behind this keyword plus the parentheses, so it means calling a class of its own constructor, in order to explain this usage we change it again Person class.

Java language keyword usage of this comprehensive summary
This time, we give the Person class has added a constructor. This method of construction is only two parameters, and only initializes two attributes. For convenience tell, we constructor above three parameters called "construction method ①", the construction method of the following two parameters referred to as "construction method ②." Not difficult to find through observation, before the two construction methods among 2 lines of code is duplicated each other, in order to avoid this repetitive code is present, we can call the "construction method ②" in the "construction method ①" them. Call as follows:

Java language keyword usage of this comprehensive summary
In the "constructor ①" by "the this (parameters);" approach called "construction method ②." This is yet another use of this keyword. Many small partner may not understand why you want to call the constructor in this way? We do not directly write a "Person (name, age); " to call it? Here we must explain: In the Java language, the constructor of a class with the same name as the class. However, a class which may be the same as the definition of a "normal method" class name, in other words: not only the class constructor with the same name, "general method" can also take the class and the same name (but full programmers in the world will not do it). So, in this case, the compiler of this approach is how to distinguish between "normal method" or "constructor" mean? Very simple, "conventional method" in front of the return type name must be defined, the "constructor" in front of the name of the type definition there is no return value. In this way, the compiler will be able to distinguish what is "constructor", which is "a common method and class of the same name."
When distinguish the definition, but at the time of the call, is through the method name to call, then how can distinguish code Which statement is called "constructor", which sentence is called "name of the class common method "mean? To solve this problem, Java language requirements, call the constructor in this class, the need to call through "this (argument)" approach. In addition, Java language also provides rules called in a way that must be followed. First, the "the this (parameter)" means only "other constructor" used, not by the conventional method. If you use this way in the conventional method, it will be treated as a syntax error, as shown below
Java language keyword usage of this comprehensive summary
can be seen in the conventional method in such a way as to call the constructor method will be problems.
Second, a constructor, the form "this (argument)," the call to the constructor method, "this (argument)" must be written in the first line of the calling method. Third, each cycle nested calls can not appear, that is, can not call the constructor in ① ② in the constructor, and simultaneously call the constructor ① ② in the constructor, shown below
Java language keyword usage of this comprehensive summary
wording of this cross call absolute not allowed.

Next, let us ponder two questions: First, the implementation of the main () method to the "new Person (" Joe Smith ", 20,178.5);" When this code is actually calling the constructor ①, while the construction method ① it also calls the constructor method ②, two constructors have been called up, it will create two Person objects in memory of it? The answer is no, why? The reason is simple: constructor ① ② call the constructor, and no new keywords, call the constructor to initialize ② just completed the name and age of these two properties, and does not create two objects.

We need to think about the second question is: Since the same Java language allows "ordinary method" in the name of the class name, and the construction method is also the same as the class name, then the class other than the Person If you write them on a "Person (parameters) "this code, called a virtual machine to determine how an ordinary method or constructor do? The answer is simple, if the previous "Person (parameters)," the emergence of new keywords, which indicates that the constructor is called, otherwise the call is explained ordinary method.

Talking about this, we put this the most common types of usage of keyword finished, in fact, this keyword when we write the code inside the class, there is a purpose, and that is specifically attributable to distinguish between property or method. We look at the following code:
Java language keyword usage of this comprehensive summary
In the code, the class also defines the external Outter, a Outter a property, and are also defined Outter inner classes, called a class attribute in the external printA inner class () method . We all know that an inner class can directly access its properties and methods from outside the class. This feature has been reflected in our above this code. However, if there is a property or method with the same name as an external class inner class, how to distinguish the call in the end is which property or method of it? For example, in Inner class also appeared in a property, then the output of a statement which in the end means a do? Quite simply, if the output directly to write a statement, then the call is an internal class of a property. In order to emphasize that it is an internal class of a property, we can also add this keyword in front of a. If we want to call an external class of a property can be invoked with the "outside the class name .this.a" way, as shown below:
Java language keyword usage of this comprehensive summary
Above we put the significance of this keyword and its various uses of finished , briefly summarize:
1, the this: express itself an object, that is, this object itself
2, this attribute name: representation of this object's own properties
3, this method name: representation of this object's own method
4, this (parameters) this represents the object itself constructor (Note: "constructor" concept is relative to the "class" is concerned, but specific to this (argument) this usage, said, "I own this object's constructor")
5 ., outside the class name .this property: that invoked within the class is a property outside of class (call an external class method likewise)

I hope this can help beginners in-depth understanding of the role of this keyword.

If you want to learn Java programming system, and welcome to my video lessons on this site.

Guess you like

Origin blog.51cto.com/2266836/2483097