Given the class definition below. How would one go about deciding whether the stub methods should be static or non-static?
class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
// Should the methods add(), subtract() and inverseOf() be non-static ...
public Point add(Point point) {
}
public Point subtract(Point point) {
}
public Point inverseOf() {
}
// Or static?
public static Point add(Point point1, Point point2) {
}
public static Point subtract(Point point1, Point point2) {
}
public static Point inverseOf(Point point) {
}
}
I would go for instance methods. You then have the capacity of making the methods part of an inteface and override them. You would get the benefit when you have to deal with 2d points or 3d points and have some client code that doesn’t really care and just need to perform operations on Points implementing the interface.