Use of the super and the This keyword in Java

Source: Internet
Author: User

A This keyword

The This keyword can appear in a constructor method and an instance method, not in a static method, because a static method can be called with the class name, and there may not be any objects present.

This is used in two main ways:

1 in the constructor method, call the other constructor methods of this class.   The format is this ([argument list]); When you call the other constructor methods of this class through this, the statement must appear in the constructor method and must appear as the first statement.

2-generation refers to the current object

For example:

1  Public classPerson {2 3 PrivateString name;4 5 Private intAge ;6 7 PrivateString gender;8 9  PublicPerson () {//Construction Method OneTen  One }         A  -  PublicPerson (String name,intAge) {//Construction Method Two -  the  This(Name, age,NULL);//Use the This keyword to invoke the constructor method in this class three -  - } -  +  PublicPerson (String name,intAge1, String gender) {//Construction Method Three -  +  This. Name =name; Because the parameter is the same as the member variable name in the class, this.name is used to represent the member variable A  atAge =Age1; Because the parameter differs from the member variable name in the class, it can be impractical this.age, directly with age = Age1 -  -  This. Gender =gender; -  - } -  in}

Two Super Keywords

There are two uses of the Super keyword:

1 using the Super keyword in the constructor method of a subclass to invoke the constructor of the immediate parent class, the Super statement must be the first sentence in the constructor method of the subclass. If you do not use the Super keyword in a subclass, the system defaults to calling the parent class with no arguments, that is, the implicit use of super (), the statement, and notice that the parent class must have a parameterless construction method.

2 Use the Super keyword to invoke member variables and methods that are hidden by the quilt class. If a child class defines a member variable that has the same name as the parent class, the subclass hides the member variable that inherits from the parent class. If a child class defines a method that is exactly the same as a method defined by the parent class, the method inherited from the parent class is completely hidden (i.e. override, overwrite).

Use of the super and the This keyword in Java

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.