Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Java in General and the fly likes Initiaizing the reference varibles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Initiaizing the reference varibles" Watch "Initiaizing the reference varibles" New topic

Initiaizing the reference varibles

Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Though I am not in any mood to start controversy here but looking for the best way to initialize the variables when we don't have the values to be assigned at run time.
In this situation, I can either initialize reference variables to Null, empty or left them uninitialized.

Though local and final variables will give compilation errors if left uninitialized so they have to be either empty or null initialized.

What is you call on the following ways of initializing variables. And how to handle erroneous conditions in a program regarding these initializations. Is it good to place null checks or checks for empty initializations.


Java Experience
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 41531
Do you mean only local variables?

What do you mean by reference variables? Do you mean fields? What does Null, empty or ... uninitialised mean?

If you have values which are uninitialised at runtime, why do you have those variables in the first place? If you have a final field which you are initialising to null, that looks like poor design to me. Get rid of whichever constructor initialises those values to null.
I agree. Here's the link:
subject: Initiaizing the reference varibles