In Java, which constructor will be choosen when parameter is null [duplicate]

This question already has an answer here:

Let’s say I have this code:

public class A{
    public A(String b){}
    public A(Cursor c){}
    public A(SomeClass n){}

Now I want to create an instance of A but I don’t know which constructor will select when the only parameter is null:

A someA = new A(null);

How should I manage this and figure it out ?


I have test this, it wont be compile with null but when I use a null object it will consider what is type of object like:

String n = null;
A someA = new A(n);

it will execute public A(String b){}

Is that safe ?

Source: oop

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.