Java basics of this and super keyword usage

Source: Internet
Author: User

This

In Java, this is a reference variable that refers to the current class object, mainly in the following ways:

Referencing an instance variable of the current class ·

The This keyword can be used to refer to an instance variable of the current class. If there is ambiguity between the instance variable and the parameter of a method (the instance variable and the parameter name are the same), the variable name can be used to explicitly specify the instance variable to resolve the ambiguity problem.

Calling the current class method

You can use the This keyword to invoke the method of the current class, in the format: this. Method name. If you do not use the This keyword, the compiler will automatically add this the this keyword when calling a method.

Call the constructor of the current class

The This () constructor call can be used to invoke the constructor of the current class (parameters can be taken in parentheses). It is used to reuse constructors, the This () constructor call is used to reuse a constructor from a constructor, which maintains the chain between constructors, that is, it is used for constructor links.

Passed as a parameter to a method or construct method

The This keyword can also be passed as a parameter in a method, primarily for event handling. In case of event handling, you must provide a reference to one class to another. It is used to reuse an object in multiple methods.

Used to return an instance of the current class

You can return from the This keyword in a method as a statement. In this case, the return type of the method must be a class type (not raw).

Super

The Super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever an instance of a subclass is created, an instance of the parent class is implicitly created and referenced by the Super keyword reference variable.

Referencing direct parent class instance variables

You can use the Super keyword to access the data members or fields of the parent class. If the parent and child classes have the same field, use Super to specify the data member or field as the parent class.

Calling the parent class method

The Super keyword can also be used to invoke a parent class method. If the child class contains the same method as the parent class, you should use the Super keyword to specify the method of the parent class. In other words, if the method is overwritten, you can use the Super keyword to specify the parent class method.

Calling the parent class constructor

The Super keyword can also be used to call the parent class constructor. If there is no constructor, the compiler automatically provides a default constructor. However, it also adds super () as the first statement.

Reprint please specify the source https://www.cnblogs.com/Y-oung/p/9119300.html

To work, study, communicate or have any questions, please contact email: [Email protected]

Java basics of this and super keyword usage

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.