I’ve been going through the Android development tutorials and I see a lot of calls like this:
String date = new StringBuilder().append(mMonth + 1).append("-").append(mDay).append("-").append(mYear).append(" "));
Seems like a nice shorthand way of doing things, but is this really a good practice? Will this have any negative side effects?
It’s called a fluent interface.
The code does not call methods during the call to the constructor. It calls methods on the object returned by the constructor, after the constructor has completed.
Each of those method calls finish with
return thisand that is the key. That’s what allows the calls to be chained together.