Returning
this allows several calls on the same object to be "chained". Most often I see this used with a
StringBuilder:
This is functionally identical to the following code:
but requires less typing and, at least for some people, is more readable (this is obviously subjective).
Note that the
append() methods in the second example returns
this as well, but the caller just doesn't use it. You aren't required to "use" the value returned by a method in
Java, but returning "this" allows to chain the methods as outlined in the first example.
Returning
this is also used by so called
Fluent interfaces. However, the concept of fluent interfaces is broader than just returning
this - fluent interfaces always return the instance that is best suited for the particular context of the operation that was performed. (Uh, I hope the Wikipedia article makes better job of explaining it than I did here.)