java this keyword

前端 未结 7 1500
野的像风
野的像风 2020-12-11 09:00

I have read that in Java you don\'t have to explicitly bind the this keyword to object, it is done by interpreter. It is opposite to Javascript where you

7条回答
  •  北海茫月
    2020-12-11 09:53

    this keyword is always used in referencing the object of the current class. where as this() is used for the current class constructors. for example:

        class circle {
            
            public int radius;
            public Circle() {
               this.radius = 10; //any default value
            }
    
            public Circle(int radius) {
               this.radius = radius  // here this.radius will set instance variable radius
            }
            public int areaOfCircle() {
                  return 3.14*radius*radius;
            }
        }
    

提交回复
热议问题