• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

question about using "new"

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
    Bookmark Topic Watch Topic
  • New Topic