Can Constructor Return a Null Object

Can constructor return a null object?

The code is dead in any version of Java. It's not possible for a constructor to return null, and even if an exception would be thrown from the constructor, the next line won't be called.

Can a constructor return a NULL value?

I agree with everyone else that you should use exceptions, but if you do really need to use NULL for some reason, make the constructor private and use a factory method:

static CMyClass* CMyClass::create();

This means you can't construct instances normally though, and you can't allocate them on the stack anymore, which is a pretty big downside.

Alternative to constructor returning null

Constructors define the initialization of an arbitrary object. Thus they could never return values.

I would argue that the lack of a hasElement method is a major design flaw, but nevertheless if you insist on returning null, you can.

In any case you should leverage generics and Method references (>= Java 8).

You could use the wrap method in the code example for your given requirements (Using inheritance to allow sub classes to read children easier).

public void readElement(Element element) {
this.bar1 = wrap(element.getChild("bar1"), Bar::new);
this.bar2 = wrap(element.getChild("bar1"), Bar::new);
this.bar3 = wrap(element.getChild("bar1"), Bar::new);
}

protected <T> T wrap(Element element, Function<Element, T> elem) {
if (element == null) {
return null; // Not a good value
}
return elem.apply(element);
}

Is there a way to have a constructor return an object that == null in Java?

Typically you would just have it throw an exception for this situation. You can easily write your own exception or just use something like throw new Exception("foo failed to initialize"); and capture that.

Writing your own Exception: http://www.javaplex.com/blog/java-creating-custom-exceptions/

What conditions cause object instantiation to return null?

It's impossible for new to return null, assuming the VM is functioning correctly.

From section 15.9.4 of the Java Language Specification:

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

Can object constructor return a null?

In my opinion, what the else statement suggests is that the previous developers didn't know their C#. A constructor always returns a constructed object or throws an exception.

In the very old times, C++ constructors could return null, so maybe the problem comes from that. This is no longer true in C++ either, at least for the default new operator.

Does constructor returns null?

In regular sane code, a constructor will not return null. There are some convoluted ways you can force a constructor to return null, but it is such a bizarre edge case that you will never see it. To all intents and purposes: new on this object will never return null - and it is completely pointless to add a null-check after a new(), especially for something sensible like StreamReader.

A simple case where you can get null:

object obj = new int?()

But this is simply exposing the subtle boxing behavior of nullable types. The more complicated way of getting a contructor to return null requires evil:

static void Main() {
var obj = new MyFunnyType(); // wow! null!
}

class MyFunnyProxyAttribute : ProxyAttribute {
public override MarshalByRefObject CreateInstance(Type serverType) {
return null;
}
}
[MyFunnyProxy]
class MyFunnyType : ContextBoundObject { }

Java object constructor returning null

Is the other way around, instead of:

public User(String Fname, String Lname, String Mail, long num, long card){
Fname=FirstName;
Lname=LastName;
Mail=Email;
num=PhoneNum;
card=CardNum;
}

it should be:

public User(String Fname, String Lname, String Mail, long num, long card){
FirstName = Fname;
LastName = Lname;
Email = Mail;
PhoneNum = num;
CardNum = card;
}

To avoid this kind of thing you can use the this keyword. Here is an example:

String firstName;
public Constructor(String firstName){
this.firstName = firstName;
}


Related Topics



Leave a reply



Submit