Java Basics Summary (ii)---keywords

Source: Internet
Author: User

1.this

In Java, the This object, which can refer to the class's properties and methods within the class.

1. It is used inside a method, that is, a reference to the object to which this method belongs.

2. It is used inside the constructor to represent the object that the constructor is initializing.

3.this represents the current object, and you can invoke the properties, methods, and constructors of the class.

The object that calls the method is required within the method, and this is used.

 Public classThisdemo {intnum; Thisdemo increment () {num++; return  This; }    Private voidprint () {System.err.println ("Num=" +num); }     Public Static voidMain (string[] args) {Thisdemo TT=NewThisdemo ();    Tt.increment (). Increment (). Increment (). print (); }    }

This keyword returns itself to this object and then implements multiple operations in a single statement.

Use this to call the constructor of this class

 Public classThisDemo2 {String name; intAge ;  PublicThisDemo2 () { This. age=21; }          PublicThisDemo2 (String name,intAge ) {         This();  This. Name= "Mick"; }       Private voidprint () {System.out.println ("Final name =" + This. Name); System.out.println ("Final age =" + This. Age); }     Public Static voidMain (string[] args) {ThisDemo2 TT=NewThisDemo2 ("", 0);//the parameters that are passed in randomlyTt.print (); }}

Java Basics Summary (ii)---keywords

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.