File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialising objects and Strings in local methods

 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • 0
  • 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.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • 0
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic