This keyword in the class, this keyword

Source: Internet
Author: User

This keyword in the class, this keyword

This can be used to distinguish between local variables and member variables, because if this. member variable = parameter value is used in the constructor,

The initialization value can be assigned to the member variable when the new object is created. Otherwise, the assignment fails,

Therefore, this can distinguish between member variables and local variables.

Class person {private String name; person (String name) {name = name; // The default value of the member variable name is null, assign a value to the local variable} person (String n) {name = n; // The member variable is assigned a value at this time} // when the value is assigned, if the parameter name and member variable name are the same, find the local variable and assign a value. If they are different, find the member variable. If the assigned object is not a member variable or a parameter name, the error "person (String name) {this. name = name; // normal value }}

This feature: when an object calls the function of this, this indicates the object, that is, this actually refers to the specific object with new.

Use this to call other constructors in the constructor: this (list of constructor parameters to be called); and The this () statement should be placed in the first row of the constructor that calls other constructor.

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.