In java, you can always create a free-standing function as a static method of one of two classes (or the third helper class).
I donβt think that there is a preferred way at all, but you could use all three for more flexibility and reuse the code.
For example, one method will contain logic:
class Point { ... float distToLine (Line l) { .... return some result; } ... }
and then other methods will call the original method:
class Line { ... float distToPoint (Point p) { return p.distToLine (this); } ... static float pointToLineDistance (Point p, Line l) { return p.distToLine (l); } ... }
source share