Uninitialized object versus object Initialized to NULL

I am working in Java.

I usually configure some objects as such:

public class Foo { private SomeObject someName; // do stuff public void someMethod() { if (this.someName != null) { // do some stuff } } } 

The question arises: Is someName in this example equals null , as-in. Can I reliably for all objects assume that null checks on uninitialized objects will be exact?

+62
java null initialization
May 22 '13 at 18:57
source share
2 answers

Correctly, both static and instance members of a reference type that are not explicitly initialized are set to null using Java. The same rule applies to array members.

From the Java Language Specification , Section 4.12.5:

Initial Variable Values

Each variable in the program must have a value before its value:

Each class variable, instance variable, or array component is initialized to a default value when it is created.

[...] For all types of links, the default value is null .

Please note that the above rule excludes local variables: they must be initialized explicitly, otherwise the program will not compile.

+82
May 22 '13 at 18:59
source share

If the Object reference was declared but not created, its value is null .

+8
May 22 '13 at 18:59
source share



All Articles