Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is unitialized object in Java not set to null?

I’m having a very basic problem here which I was not able to find a solution to even though it must exist.

I have the following code:

public class Foo {
    public static void main(String[] args) {
        String foo;
        if (foo != null) {
            System.out.println("foo");
        } else {
            System.out.println("foo2");
        }
    }
}

It gives me

''variable might not have been initialized''

. Why is it that I have to assign null explicitly and not all variables are initialized with null by default?

like image 921
aufziehvogel Avatar asked Dec 22 '11 09:12

aufziehvogel


People also ask

Are uninitialized objects null?

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.

Can we initialize object to null in Java?

You should initialize your variables at the top of the class or withing a method if it is a method-local variable. You can initialize to null if you expect to have a setter method called to initialize a reference from another class. You can, but IMO you should not do that. Instead, initialize with a non-null value.

Are objects null by default?

In Java, class and instance variables assume a default value (null, 0, false) if they are not initialized manually. However, local variables aren't given a default value. You can declare but not use an uninitialised local variable. In Java, the default value of any object is null.

What happens in Java if you try to use an uninitialized variable?

Local Variables If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error. Save this answer.


1 Answers

It prevents you from accidentally using a local variable before it's been definitely assigned. This is a good thing - it helps to prevent bugs. It's a shame that it can't be done for fields (instance and static variables) - but other than final variables, the compiler doesn't know when you're going to read them vs when you're going to assign them - it can't tell the order in which you're going to call methods. Within a single method, it has much more information about the flow control, so can provide more checking for you.

If you want the value of a local variable to be null, why don't you just explicitly set it to null? That not only keeps the compiler happy - it makes it clear what your intentions are too.

See chapter 16 of the Java Language Specification for more details on definite assignment.

Here's an example of how it can prevent bugs:

Iterable<String> collectionOfNames = ...; // Some method call
String lastNameInCollection;
for (String name : collectionOfNames)
{
    lastNameInCollection = name;
}
System.out.println("The last name was: " + lastNameInCollection);

What should happen if the collection is empty? Maybe we want to print "The last name was: null" - but maybe we should do something else. The fact that the above won't compile (as lastNameIncollection may not be assigned) forces the programmer to think about the case where the collection of names is empty.

like image 61
Jon Skeet Avatar answered Nov 12 '22 10:11

Jon Skeet