This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Initialising objects and Strings in local methods  RSS feed

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just wondering what the generally accepted method for initializing objects and Strings are in local methods? I usually initialise to null, but have found issues doing this for the String object, problems obviously concatating, etc. Just wondering what's the generally accepted practice is? Thanks.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark O' Sullivan wrote:Just wondering what's the generally accepted practice is? Thanks.



The best way is to declare a variable as close as possible to where it's actually going to be used. Then its obvious what value you should initially assign to it. It's determined by the algorithm you're implementing.
 
Marshal
Posts: 60095
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will rarely need null like that. Only for things like Readers and (SQL) Connectors which ought to be closed in a finally block. They often have to be initialised to null immediately before the first try.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!