Javase Study Notes]-7.7 details and applications of this

Source: Internet
Author: User

Javase Study Notes]-7.7 details and applications of this

This section is followed by the previous section to continue learning the this keyword.

One of the details that should be paid attention to in the constructor in section 7.5 is that constructor can call general functions, but general functions cannot directly call constructor. however, we have no idea whether the constructor can call the constructor. Now let's take a look at this problem.

So how does one access constructor and constructor?

To grasp this problem, we must clarify the concept that if a member in a class is to be executed, it must be called by the object. Each object has a corresponding this.

The java language provides the keyword "this", which is to use this to call the constructor and use different parameters to call different constructor.

Let's look at an example:

Class Person {private String name; private int age; Person () // constructor 1 {System. out. println ("person run");} Person (String name) // constructor 2 {this. name = name;} Person (String name, int age) // constructor 3 {this. name = name; this. age = age;} public void speak () {System. out. println (this. name + ":" + this. age );}}

Class ThisTest {public static void main (String [] args) {Person kobe = new Person ("kobe", 37); kobe. speak ();}}
For the above example, we can see that the first statement in constructor 3 has actually been implemented through constructor 2, so in order to improve code reusability, why don't we call constructor 2? in java, the following statements are used to call constructor:

Person (String name, int age) // constructor 3 {this (name); // uses the this keyword to call the constructor Person (String name) this. age = age ;}
We can see that this keyword can be used to call other constructors in the constructor. Of course, for the constructor to be called, it is determined by parameters.

Then we will clarify the call form between the constructor and the constructor.

Next, let's take a look at two details that need to be paid attention to in the usage of the two this keywords:

The first detail: the constructor called in the constructor can only be defined in the first line of the constructor.

This is why, because the initialization action must be executed first. This is a rule defined by the java language. If it is not defined in the first line, compilation will not work.

Let's take a look at the example to switch the statements in constructor 3 above:

Person (String name, int age) // constructor 3 {this. age = age; this (name); // The constructor Person (String name) is called using the this keyword )}
Let's look at the compilation:

We see a compilation error: the call to this must be the first statement in the constructor. this is the first detail when we use the this keyword.

Second details: Pay attention to the recursive loop that occurs when calling the constructor, leading to internal stack overflow.

Let's look at an example:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> Person () // constructor 1 {this ("KOBE"); System. out. println ("person run");} Person (String name) // constructor 2 {this (); this. name = name ;}

Result:


We can easily find that the two constructors call each other to form recursion, so that the two constructors continuously go into the stack, the stack memory overflows, and the program ends.

We have read two details that need to be paid attention to when using the this keyword. Next, let's take a look at the key application of this.

When do we usually use this? We mentioned the concept of this in the previous section: this indicates the object, which is the reference of the object where the function is located. it is easy to understand that when we use the object of this class in a class, we usually use this to reference it.

Let's implement a function: Determine whether two people are of the same age.

Analysis: To determine whether two people are of the same age, we only need to compare the two people (that is, two Person objects) to see if they are of the same age, that is to say, an object can directly call the method in the Person class to compare it with another object.

We can achieve this:

// Determine whether two people are of the same age public boolean compare (Person p) {return this. age = p. age ;}
Let's test:

Class ThisTest {public static void main (String [] args) {Person kobe = new Person ("kobe", 37); Person james = new Person ("james", 31 ); kobe. speak (); james. speak (); System. out. println ("are the two equal?" + kobe. compare (james ));}}
Result:


We can see that the main application of this is to represent the object. When the object calls all the functions of this, we generally think that this represents the object.


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.