Understand the meaning of this and static in Java

Source: Internet
Author: User

Understanding the meaning of this and static in Java strong http://blog.csdn.net/microtong

// This: reference of the current object
// When referencing a member variable of a class, this is implicitly passed by the compiler.
// Usage of this
// Return the reference of the current object
// Call another constructor from one Constructor
// Differentiate member variables and parameter variables with the same name

// Static variable: Class variable, shared by all objects of this class
// Static method: the static method does not contain this reference.
// You cannot access non-static member variables and methods in static methods.
// You can directly access static members through a class, even if the class object does not exist.

 

Package cn.edu. UIBE. oop; </P> <p> // This: reference of the current object <br/> // When referencing a member variable of a class, this is implicitly passed by the compiler <br/> // usage of this <br/> // returns a reference to the current object <br/> // calls another one from a constructor constructor <br/> // differentiate member variables and parameter variables with the same name </P> <p> // static variables: class variable, share all objects of this class <br/> // static method: the static method does not reference this. <br/> // you cannot access non-static member variables and methods in the static method. <br/> // you can directly access static members through the class, even if the object of this class does not exist </P> <p> public class point {<br/> Public Double X; // X axis coordinate <br/> Public Double Y; // y axis coordinate <br/> Public static int counter = 0; // number of points <br/> Public point () {<br/> This (1.0, 1.0 ); // use this to call other constructor methods <br/>}< br/> Public point (Double X, Double Y) {<br/> This. X = x; // This. X is a member variable, and X is a parameter variable <br/> This. y = y; <br/> counter ++; <br/>}< br/> Public point plus () {<br/> This. X ++; <br/> Y ++; <br/> return this; // return the reference of the current object <br/>}< br/> Public void show () {<br/> system. out. println ("x =" + x + "Y =" + Y); <br/>}< br/> Public static void showcounter () {<br/> system. out. println ("number of points:" + counter); <br/> // X = 4; // unable to access the instance variable <br/> // This // no reference for this <br/> // show (); // non-static methods cannot be called <br/>}< br/> Public static void main (string [] ARGs) {<br/> Point P1; <br/> P1 = new point (); <br/> point P2 = new point (3, 3); <br/> p2.plus (); <br/> p1.show (); <br/> point P3 = p1.plus (); // P3 and P1 point to the same object <br/> If (p1 = P3) {<br/> system. out. println ("p1 = P3"); <br/>}< br/> p1.plus (). plus (). plus (); <br/> p1.show (); <br/> p2.show (); <br/> system. out. println (point. counter); <br/> point. showcounter (); // call a static method by class name <br/>}</P> <p >}< br/>

 

Understanding the meaning of this and static in Java strong http://blog.csdn.net/microtong

 

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.