Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Declaring an object as null

Thread view: 
bobby_b_ - 15 Jun 2006 16:23 GMT
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?
Thomas Hawtin - 15 Jun 2006 16:38 GMT
> 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;
[quoted text clipped - 4 lines]
>
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

jmcgill - 15 Jun 2006 17:08 GMT
> 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.
[quoted text clipped - 7 lines]
>
> 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.
Oliver Wong - 15 Jun 2006 17:16 GMT
>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.
[quoted text clipped - 7 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.