permaculture playing cards*
The moose likes Beginning Java and the fly likes Initialising objects and Strings in local methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initialising objects and Strings in local methods" Watch "Initialising objects and Strings in local methods" New topic
Author

Initialising objects and Strings in local methods

Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
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

Joined: Oct 22, 2009
Posts: 237
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
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.
 
 
subject: Initialising objects and Strings in local methods
 
Similar Threads
equals( )
java5 .0???????
Simple object question
Threads - enough is enough
toString() Vs (String)