This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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: 62192
193
  • 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.
 
crispy bacon. crispy tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!