Declaring an object as null

B

bobby_b_

I have been told that when creating an object as a local variable, it
is better to declare the object as null, and then instantiate it.

Example:
MyClass object = null;
object = new MyClass();

instead of
MyClass object = new MyClass();

Is this true? Is it better to use that extra line?
 
T

Thomas Hawtin

bobby_b_ said:
I have been told that when creating an object as a local variable, it
is better to declare the object as null, and then instantiate it.

You can't declare an object as null (as such). null is merely the
absence of an object. You can, however, assign null to a variable.
Example:
MyClass object = null;
object = new MyClass();

instead of
MyClass object = new MyClass();

Is this true? Is it better to use that extra line?

No, it's rubbish.

In fact it's arguably better to write:

final MyClass object = new MyClass();

(The argument against, is that you there is an extra keyword in there.)

If you don't need to assign a value immediately, don't. For instance:

final MyClass object;
if (someCondition) {
object = new MyClass();
} else {
object = new MyClass(42);
}
... use object ...

The advantage here is that the compiler will complain if there is a path
through your code that does not assign the variable (there is a whole
very tedious chapter in the JLS on the subject). Making the declaration
final prevents multiple paths attempting to initialise it.

Tom Hawtin
 
J

jmcgill

bobby_b_ said:
I have been told that when creating an object as a local variable, it
is better to declare the object as null, and then instantiate it.

Example:
MyClass object = null;
object = new MyClass();

instead of
MyClass object = new MyClass();

Is this true? Is it better to use that extra line?'


It makes very little practical difference, but I prefer a third option:

MyClass object;
object = new MyClass;

Makes it simpler to move the declaration later, which happens a lot for me.

By setting the object to null, you can easily miss the compile time
error of uninitialized value, and end up with runtime NPE's instead.
For that reason, I would avoid your first example. There are situations
where you actually do want an object initialized to null, of course.
 
O

Oliver Wong

bobby_b_ said:
I have been told that when creating an object as a local variable, it
is better to declare the object as null, and then instantiate it.

Example:
MyClass object = null;
object = new MyClass();

instead of
MyClass object = new MyClass();

Is this true? Is it better to use that extra line?

As Thomas said, it's completely wrong. My guess is this is a bad habit
from the old days of C programming where a new variable would have a random
value, and the compiler could not detect uninitialized variables. By setting
the pointer to zero, you could guarantee a runtime error when you tried to
dereference that variable.

Java compilers are smart enough to detect uninitialized variables in
most cases, and can alert you at compile time if such an error occurs. By
explicitly assigning to null as above, you're defeating this protection,
thus potentially introducing more bugs in your code.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top