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