The method is fine. The problem lies elsewhere.
It could be that ball2.x == this.x
and ball2.y == this.y
. This could be because ball2
and this
are the same object, or because you forget to initialize x
and y
, or for a variety of other possible reasons.
Another possibility is that the value you're printing out is not the result of calling distance()
, but is something else (for example, because of an error in the code).