aspose file tools*
The moose likes Beginning Java and the fly likes question about using Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about using "new"" Watch "question about using "new"" New topic
Author

question about using "new"

Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Hi,

I'm just wondering what's the difference of doing this:



as opposed to this



Are there any performance implications? Which "style" is more recommended? Thanks!
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
"style" 2

owing to readability and hence better maintainability.

Also, if you need to use the instance of "IceCream" for further tasks within your class, you'll need a reference to that object, which you wont get, if you use the first way.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

They both do the same thing, your second example is better style because what would happen if you wanted to call another method on new Icecream()?






Hunter.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Hunter McMillen wrote:They both do the same thing, your second example is better style because what would happen if you wanted to call another method on new Icecream()?



Hunter.


Well, you can do that if go() returns an instance of IceCream. I'd argue that one method isn't better than the other if all you need to do is instantiate the class and/or call a single method. The whole "what happens when..." is when your tests deem it necessary and you refactor.


GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about using "new"