What is the meaning of “this” in Java?

后端 未结 21 2562
走了就别回头了
走了就别回头了 2020-11-21 05:41

Normally, I use this in constructors only.

I understand that it is used to identify the parameter variable (by using this.something), if i

21条回答
  •  北海茫月
    2020-11-21 06:06

    This refers to the object you’re “in” right now. In other words,this refers to the receiving object. You use this to clarify which variable you’re referring to.Java_whitepaper page :37

    class Point extends Object
    {
        public double x;
        public double y;
    
        Point()
        {
            x = 0.0;
            y = 0.0;
        }
    
        Point(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
    }
    

    In the above example code this.x/this.y refers to current class that is Point class x and y variables where (double x,double y) are double values passed from different class to assign values to current class .

提交回复
热议问题