Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Following is the simple code relevant to constructor overloading in Java. Let's have a look at it.

Java
package temp;

final public class Main
{
    private Main(Object o)
    {
        System.out.println("Object");
    }

    private Main(double[] da)
    {
        System.out.println("double array");
    }

    public static void main(String[] args)throws Exception
    {
        Main main = new Main(null);
    }
}


In the above code, constructors are being overloaded in which one has a formal parameter of type Object and the other has the formal parameter of type double[] (array).


Main main = new Main(null);

One of the constructors is being invoked by the above statement which is using a null value as it's actual argument and the program is displaying the output double array on the console. How does the compiler resolve a specific constructor (or a method, if such is a case) dynamically at run time in such a situation?
Posted
Comments
Lakamraju Raghuram 22-Jan-12 13:18pm    
Just to add spice, what if we have another constructor taking say int array:

private Main(int[] intArray){}

1. Will the call new Main(null) compile ( I am new to java so pardon me if it is naive
2. If yes then where it will go to the ctor with double[] or with int[] ?

[I don't have J-compiler to check (:]
TorstenH. 23-Jan-12 9:29am    
Believe it or not - I just ran exactly over this problem in my code. Had 2 methods with same naming and suddenly I wanted to address one of them with null:

public ViewerFilter getFilter(final ComponentID oID) {
if(/* condidion*/) { return getFilter(null); }
}

private ViewerFilter getFilter(final MessageType oType) {
// code to return Filter
}

The compiler didn't figure it and I got an error.

I think the answer can be found here[^], but you may need to read through it a few times. (Where's Torsten when you need him?).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jan-12 17:27pm    
But how it is it translated into this particular problem, which is easy to resolve? (I did not vote this time.)
Please see my answer.
--SA
TorstenH. 23-Jan-12 3:31am    
Interesting little thingy.
I must admit, I had to try it in my playground to see what happens.

I second both answers and do highly recommend Richards link.
You cannot use the constructor call Main(null) by only one simple reason: ambiguity. All three signatures of all three constructors match this call, as Object, double[] and String[] are reference types, so any of the parameters can be null.

To provide required semantic without ambiguity, it is unavoidable that you should add yet another constructor with district signature. If could be a constructor without parameters, a constructor with one primitive-type parameter (like Boolean, for whatever purpose), a constructor with two parameters of any type, etc.

Easy enough, isn't it?

—SA
 
Share this answer
 
v2
Comments
TorstenH. 23-Jan-12 3:31am    
Interesting little thingy.
I must admit, I had to try it in my playground to see what happens.

I second both answers and do highly recommend Richards link.
Sergey Alexandrovich Kryukov 23-Jan-12 3:50am    
This is what I do on a regular basis: trying in my playground.
I don't think this is interesting. Rather, this is a very basic stuff.
Thank you.
--SA
Richard MacCutchan 23-Jan-12 4:32am    
I agree with your comments. However, the OP's question was "how does the compiler decide which constructor to call in this instance?". Having tested this myself I'm still interested in the fact that the compiler generates a workable solution and ignores the fact that this call is ambiguous; I would have thought that it should produce a diagnostic.
TorstenH. 23-Jan-12 9:26am    
naa, it does not. It's rating and using the method that looks "best".
Richard MacCutchan 23-Jan-12 12:32pm    
How interesting, I can write some really bad code (not unusual for me) and the compiler will 'fix' it silently.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900