10

I have the following two object variables

Date a;
Date b=null;

Definitely both 'a' and 'b' are not referring to any objects.

Now if I invoke following statement

System.out.println(a.toString());

There will be a compile time error, whereas if I invoke the following statement

System.out.println(b.toString());

There will be no compile time error but there will be a runtime error. What is the reason for this and what value will be actually stored in 'b' to represent a null value?

  • 2
    asked and answered many times at SO: Why are local variables not initialized in Java?, Uninitialized variables and members in Java and in many questions linked to these – gnat Oct 2 '14 at 14:01
  • @gnat, do any other questions deal with the difference between "uninitialized" and "null"? Just because the answer is similar doesn't mean this is a duplicate question. – DougM Oct 2 '14 at 14:32
  • @DougM sure, did you read the first question I referred? "Was there any reason why the designers of Java felt that local variables should not be given a default value? Seriously, if instance variables can be given a default value, then why can't we do the same for local variables?" (FWIW it can't technically be a duplicate, simply because it's at another site) – gnat Oct 2 '14 at 14:34
  • 1
    That doesn't address the difference between "uninitialized" and "initialized as null", only "why aren't variables automatically initalized to null?" Same topic, slightly different question. – DougM Oct 2 '14 at 15:24
  • 2
    @DougM this is addressed in Difference between local variable initialize null and not initialize? – gnat Oct 2 '14 at 20:11
3

Thats because the state of local variables is controlled within its scope

 // method/loop/if/try-catch etc...
 {
   Date d; // if it's not intialised in this scope then its not intialised  anywhere
 }

Which is not the case for fields

class Foo{
 Date d; // it could be intialised anywhere, so its out of control and java will set to null for you
}

Now, why its fine to set a variable to null and use it immediately? maybe that is a historical mistake that sometimes leads to horrible mistakes

 {
  Date d = null;
  try{
  }catch{ // hide it here 
  }
  return d;
 } 

Now what's the semantic difference?

Date d;

just declares variable that can hold a reference that points to an object of type Date, however

Date d= null; 

does exactly the same but the reference is pointing to null this time, null is like any reference, it takes a space of a native pointer, that is 4 byte on 32-bit machines and 8 bytes on 64-bit machines

  • this seems to merely repeat point made and explained in a prior answer posted an hour ago – gnat Oct 2 '14 at 15:10
  • @gnat Thanks for your comment, but I don't think it is, cheers – Sleiman Jneidi Oct 2 '14 at 15:17
  • Do you mean that null is also an object stored somewhere in memory and all the object variables assigned with null are pointing to that null object. – Harish_N Oct 2 '14 at 16:47
  • @Harish.N no, I didn't say that, I said its a reference and not an object – Sleiman Jneidi Oct 2 '14 at 16:54
  • In the example you have given 'd' is a reference.. it is reference to an object of type Date...similarly if null is a reference.. to what object it refers to..? – Harish_N Oct 2 '14 at 16:57
16

There is no difference for class fields. They are null by default for objects, 0 for numeric values and false for booleans.

For variables declared in methods - Java requires them to be initialized. Not initializing them causes a compile time error when they are accessed.

What's the reason? Class fields can be modified by any method. In any order the method is invoked. All non-private fields may be modified by other classes and/or classes extending that class. Hence, there is no point in notifying about an uninitialized variable, since it may be assigned in many, many places.

Variables inside methods, however, are local and can be modified only inside the method itself. Hence it is both possible and rational to point possible mistakes. And the compiler tries to do it. If it knows the field is not initialized, it will show an error, because that's never what you want. If it is not sure - it will give a warning, so that you can make sure.

public static class Test {
    Date a; // ok 
    Date b = null; // ok

    public void test() {
        Date c;
        Date d = null;

        System.out.println(a.toString());
        System.out.println(b.toString());
        System.out.println(c.toString()); // error
        System.out.println(d.toString()); // warning
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.