Friday, June 26, 2009

Chaining methods using power of "return this"

In one of my older post [here], I discussed about the power of Fluent Interfaces. We can use those in building method chaining, thus improving the readability of code just english-like sentences.

We can also implement this using "return this" in class methods.

Take a look at below source code:

Bug.java



CreateBugs.java



Output:



The above programs are self explanatory :)


_________________________________________________________________________________________________________________

"Look at the sky. We are not alone. The whole Universe is friendly to us and conspires only to give the best to those who dream and work."

- Dr. A P J Abdul Kalam
_________________________________________________________________________________________________________________