Reputation: 153
I am learning Java, and going through the documentation.
There is a line on this page which I am not able to understand -
... Also, class methods cannot use the this keyword as there is no instance for this to refer to. ...
I thought that it was only static class methods which could not use the this
keyword.
To test this, I wrote the following, which compiles.
import java.math.*;
class Point {
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public double getDistanceFromOrigin() {
return Math.sqrt(this.x*this.x + this.y*this.y);
}
}
I have a class all right, in which a method refers to this
.
Am I misinterpreting things in some way?
Upvotes: 1
Views: 199
Reputation: 31194
After reading the content at the link you've posted, It seems to use the verbiage Class methods
to refer to static methods
Class Methods
The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in
You can't use this
in static methods because there is no instance(no this
) to reference.
Upvotes: 1
Reputation: 49372
You are using this
inside an instance method which will refer to the current instance.
public double getDistanceFromOrigin() {
return Math.sqrt(this.x*this.x + this.y*this.y);
}
If you change the method to a static method , this
will not be available , because static methods are tied to the class and not to a particular instance of the class whereas this
refers to the current instance of a class if used inside a method.
public static double getDistanceFromOrigin() {
return Math.sqrt(this.x*this.x + this.y*this.y); // compilation error here
}
Upvotes: 1
Reputation: 53598
Class methods are static methods. A "class method" is a method that is bound to the class definition (using the static
keyword), as opposed to object/instance methods, which you write so that you can call them on objects you build based on that class.
The code you've written has two object/instance methods, and no class methods. If you want a class method in Java, you make it static, and then you cannot use this
.
Upvotes: 5
Reputation: 6191
You are correct, only static
class methods cannot use the this
keyword, but your code example is non-static so this
is perfectly valid.
Upvotes: 1
Reputation: 85779
I thought that it was only
static
class methods which could not use thethis
keyword.
You're right. The static
methods in a class belong to the class, not to the object reference. So to prove your sentence, just add a static
method and use this
keyword in it. For example:
class Point {
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public double getDistanceFromOrigin() {
return Math.sqrt(this.x*this.x + this.y*this.y);
}
public static double getDistanceBetweenPoints(Point point1, Point point2) {
//uncomment and it won't compile
//double result = this.x;
//fancy implementation...
return 0;
}
}
Upvotes: 1