Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which constructor is called first while passing null in the class having overloaded constructor?

Below is the java class having 3 overloaded constructors :

public class Test {

    public Test(Object i){
        System.out.println("Object invoked");
    }

    public Test(String i){
        System.out.println("String invoked");
    }

    public Test(int k){
        System.out.println("Integer invoked");
    }

    public static void main(String[] args) throws Exception {

        Test t = new Test(null);
    }
}

If null value is passed while creating the new instance of class, which constructor will be invoked ? What is the reason ?

like image 615
Kshitij Jain Avatar asked Sep 14 '13 15:09

Kshitij Jain


People also ask

What is overloaded constructor?

Overloaded constructors essentially have the same name (exact name of the class) and different by number and type of arguments. A constructor is called depending upon the number and type of arguments passed. While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.

What is a constructor constructor overloading and copy constructor in Java?

After method overloading, here we come up with the concept of Constructor Overloading in Java. It is the process of declaring the same constructor with various parameters in the same class. Here, we will also discuss the copy constructor in Java with example.

What is overloaded constructor in Java?

Constructor overloading in Java refers to the use of more than one constructor in an instance class. However, each overloaded constructor must have different signatures. For the compilation to be successful, each constructor must contain a different list of arguments.

Can we pass null in constructor in Java?

The access to a null reference generates a NullPointerException. It is not allowed to pass null as a value to call the methods that contain any primitive data type.


1 Answers

Java always chooses the most specific method (or constructor) that would be applicable for the argument you pass. In this case, that's the String constructor -- String is a subclass of Object.

Think about what would happen if you had

new Test("some string")

Both the Object and String constructors would be applicable here. After all, the argument is both an Object and a String. However, it is clear that the String constructor will be invoked because it is more specific than the Object constructor and still is applicable given the argument.

null is no different; these two constructors are still applicable, and the String constructor is still chosen over the Object one for the same reason.

Now, if there were two equally "specific" constructors present (e.g. if you had an Integer constructor) there would be a compilation error when you call Test(null).

This is outlined in more detail in JLS §15.12.2:

The second step searches the type determined in the previous step for member methods. This step uses the name of the method and the types of the argument expressions to locate methods that are both accessible and applicable, that is, declarations that can be correctly invoked on the given arguments.

There may be more than one such method, in which case the most specific one is chosen. The descriptor (signature plus return type) of the most specific method is one used at run time to perform the method dispatch.

The explicit process of determining which method is the most specific is outlined in JLS §15.12.2.5.

like image 77
arshajii Avatar answered Nov 10 '22 00:11

arshajii