aspose file tools
The moose likes Beginning Java and the fly likes question about using Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 490

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
Ranch Hand

Joined: Jul 11, 2001
Posts: 15230

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: question about using "new"
 
Similar Threads
Best way to split a String on it's Capitals
ENUM
Doubt on Enums
key word static against constructor
Determine logical pattern for loop