This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The thing you're asking is pretty simple - in the case of the written code the author doesn't need the object instance any more so he/she is using the shorter syntax for the call.
So, in your example you create the object and reference it with a variable oWhy
and then invoke the method on that object
In the book example case, the created object is not assigned to variable, but is directly used only to invoke the method.
After this line this object is unreachable so if we would like to use it again we would have to instantiate it again.
Take look at the chapter dealing with Strings and its methods, you should see more examples like this dealing withe the piping the methods one to another, basically it's a same thing - only here first called method is a constructor, and constructors have different syntax for invocation.
SCJP done, SCWCD on the way!
Kok Lum Chan
Joined: Apr 12, 2009
Thank your for your clear explanation. It make sense to me now.