(fifth) Summary of the role of Java object-oriented this

Source: Internet
Author: User

The This keyword always points to the object that called the method.

This can represent any object, and when this appears in a method body, the object it represents is indeterminate, but its type is deterministic, and the object it represents can only be the current class (that is, that class in that class), only if the

When a method is called, the object it represents is determined. Who is calling this method, this is on behalf of WHO.

Refers to the data that the constructor is initializing in the constructor

Refers to the object in the method that called the method

The most important role of the This keyword is to allow one method in the class to access another method or instance variable in the class.

Class dog{    void Jump () {System.out.println ("Executing jump () method"void//  This reference calls the object in the Run () method
This represents the run () object
This. Jump ();}}

This represents the object that the constructor is initializing in the constructor.

 Public classtest{//define a foo member variable     Public intfoo;  PublicThisincontructor () {//define a foo local variable in the constructor        intFoo = 0; //Use this to represent the object that the constructor is initializing//The following code sets the Foo member variable of the object that the constructor is initializing to 6         This. Foo = 6; }      Public Static voidMain (string[] args) {//Output 6System.out.println (Newthisincontructor (). Foo)}}

If a method has a local variable with the same name as a member variable, and the program needs to access the overridden member variable, you must use this.

PublicClasstest{Private String name = "Li Gang";PrivateStaticDouble Price = 78.0;PublicStaticvoidMain (string[] args) {int price = 65;//Output 65, the local variable overrides the member variableSYSTEM.OUT.PRINTLN (price);// output 78.0 System.out.println (test.price); // Run info () method new test.info ();} public void info () {String name = "Xiaoming"; // output xiaoming System.out.println (name); // Use this to qualify//output of the name variable  , Li Gang System.out.println (this. name);}}    

(fifth) Summary of the role of Java object-oriented this

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.