Can a constructor return a null object?

Looking through the old code, I came across this stone:

MyObject o = new MyObject("parameter"); if (o == null) o = new MyObject("fallback parameter"); 

The second line is marked as dead code in Eclipse, and I understand why. No exception is explicitly thrown, and the MyObject constructor cannot throw any exception (for example, NullPointerException s).

My question is why is there a null check? Is this possible earlier in the old version of Java so that the constructor returns null? Or is it just useless and dead code?

+71
java null constructor
Jun 19 2018-12-12T00:
source share
11 answers

The code is dead in any version of Java. It is impossible for the constructor to return null , and even if an exception is thrown from the constructor, the next line will not be called.

+87
Jun 19 2018-12-12T00:
source share

No, that was never possible. Perhaps the previous version of the code used some factory method that could return null:

 MyObject o = createMyObject("parameter"); if (o == null) o = createMyObject("fallback parameter"); 
+51
Jun 19 2018-12-12T00:
source share

From section 15.9.4 of the JLS :

The value of the class instance creation expression is a reference to the newly created object of the specified class. Each time an expression is evaluated, a new object is created.

No, it can never return null.

+49
Jun 19 '12 at 14:50
source share

I assume it was written by a C programmer who is used to test the return value of malloc() for NULL , malloc() can return NULL if there is not enough memory on your system.

The code does not make sense in Java, since Java will raise OutOfMemoryError` if it runs out of memory.

+23
Jun 19 '12 at 18:17
source share

The answer is simple: the person who wrote the code was a paranoid C ++ programmer. In C ++, you can overload the new operator and use it as a simple memory allocator (aka malloc).

+8
Jun 19 '12 at 15:22
source share

It was just useless dead code. Once CTOR succeeds, you are referencing the object.

+4
Jun 19 2018-12-12T00:
source share

When you create a new Object (), you create an address in memory, and that address is not "null", but your object may be empty.

You should check for 'null' for the object passed by parameters.

+4
Jun 19 2018-12-12T00:
source share

This is just dead code.

new MyObject("parameter") will not return null in any version of java.

+3
Jun 19 2018-12-12T00:
source share

As I discovered today, despite what all the other answers say, Foo x = new Foo(...) can indeed return null if the specified code works inside a test that uses PowerMock (or some other mocking structure with similar effects):

 PowerMockito.whenNew(Foo.class).withAnyArguments().thenReturn(mockFoo); 

In this case, the code of the constructor (s) Foo is generally excluded for new Foo(...) . But if you write a test in which you do not specify the layout in the above order, you can use null instead.

But even if you use such a framework, you do not need additional code in your classes, just to gracefully handle the case that you forgot to correctly mock objects in the test! This is not a real scenario when your code is meant to run. A code that is only ever active when testing should be eliminated in any case, in which case it will only ever act for a broken test.

Thus, even if you use PowerMock, this second line should rightfully be considered "dead code" and deleted.

+2
Oct 21 '15 at 0:26
source share

If the second line is dead, and the first line is the only way to create an object, then, I think, a NullPointerException will never happen !!! (If you did not run something like 0.aMethod() . But I heard that in java even entire objects are objects, so maybe in a language where you cannot point to pointers - you cannot have pointers to a pointer .) (Lots of sarcasm in the above!)

0
Jul 14 '16 at 13:54 on
source share

This has already happened to me when I override the toString method ...

 public String toString(){ return description; } 

not sure yet.

-one
Nov 02 '16 at 7:46
source share



All Articles