Java This experience

Source: Internet
Author: User
Tags access properties

When defining a variable with a class name, the definition should be just a reference, which can be used to access properties and methods within the class, and should there be a reference in the class to access its properties and methods? Oh, Java provides a good thing, is the this object, it can be in the class to reference the properties and methods of the class

The Java keyword This can only be used in the method body. When an object is created, the Java Virtual Machine (JVM) assigns a pointer to the object that refers to itself, which is the name of this pointer. Therefore, this can only be used in non-static methods in the class, static methods and static code blocks must never appear in this, which is explained in the article "Java keyword static, final use summary". And this is only associated with a particular object, not with a class, and different objects of the same class have different this.

Package test;

Public class Thistest {

Private int i=0;

// First constructor: has an int type parameter

Thistest (int i) {

This . I=i+1; This represents the reference member variable I, and not the function parameter I

System. out. println ("Int constructori--this.i:"+i+"--"+this. I);

System. out. println ("i-1:"+ (i-1) +"this.i+1:"+ (this. ) I+ 1));

// from the results of the two outputs amply proves that I and this.i are not the same!

}

// second constructor: There is a String shape parameter

Thistest (String s) {

System. out . println ("String constructor:"+s);

}

// Third constructor: has an int type parameter and a String shape parameter

Thistest (int i,string s) {

This (s); This invokes a second constructor

//this (i);

this .=i++;

System. out . println ("Int constructor:"+i+"/n"+"String constructor:"+s);

}

Public thistest Increment () {

This . i+ +;

return This ; // Returns the current object that belongs to (thistest)

}

Public Static void Main (string[] args) {

Thistest tt0=New Thistest (ten);

Thistest tt1=New thistest ("OK");

Thistest tt2=New thistest ("OK again!" );

System. out . println (Tt0.increment (). Increment (). Increment (). I);

       return one in I++ object,

// then returns the Thistest object on the basis of the object returned above i++ !

}

}

Operation Result:

Int constructori--this.i:10--11

String Constructor:ok

String Constructor:ok again!

Int constructor:21

String Constructor:ok again!

14

Details of the comments have been written more clearly, here is not to repeat, just summed up, in fact, this is the main three ways to use:

1. represents a reference to the current object!

2, the use of class member variables, rather than function parameters, note that the function parameters and member variables with the same name are differentiated! In fact, this is the first use of the special case, more commonly used, so that come out to emphasize.

3, used in the construction method to reference the constructor that satisfies the specified parameter type (in fact, the constructor method). But there must be a lot of attention here: only one construction method can be referenced and must be at the beginning!

There is also note:This cannot be used in the static Method! So even the definition of a static method is: There is no method for this! Although exaggerated, it is fully explained that this cannot be used in the static method!

It is worth noting that:

1: In the construction call to another constructor, the call action must be placed at the starting position.

2: The constructor cannot be called within any function other than the constructor.

3: Only one constructor can be called within a constructor.

Indicate under what circumstances this is required:
First, the use of this call another constructor method, with the hair is this (parameter list), this is only in the construction method of the class, other places can not be used.
Second, in the case of a function parameter or local variable in a function with the same name as a member variable, the member variable is masked, and the member variable needs to be referenced by the "this. Member variable name" method to access the member variable. Of course, in the absence of the same name, you can directly use the name of the member variable, instead of this, use is not wrong, hehe.
Third, in the function, it is necessary to refer to the current object of the class that the letter belongs to, directly with this.

Java This experience

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.