0

Below code is running fine and printing 'Constructor B.' as an output.

public class Test
{

    public Test(Object obj)
    {
        System.out.println("Constructor A.");
    }

    public Test(String s)
    {
        System.out.println("Constructor B.");

    }


    public static void main(String args[])
    {
        new Test(null);
    }
}

But the below code is giving me compilation error 'The constructor Test(Integer) is ambiguous'

public class Test
{

    public Test(Integer obj)
    {
        System.out.println("Constructor A.");
    }

    public Test(String s)
    {
        System.out.println("Constructor B.");

    }


    public static void main(String args[])
    {
        new Test(null);
    }
}

Can someone please explain me the reason?

Anand
  • 20,708
  • 48
  • 131
  • 198
  • 1
    http://stackoverflow.com/q/5636320/2798643 It says: If you call the constructor with a String object, there's one definite constructor. However, if you instantiate new Example(null) then it could apply to either and is therefore ambiguous. – Govinda Rajbhar Feb 25 '14 at 05:22
  • @GovindaRajbhar Perfect...!!!. – Chintan Soni Feb 25 '14 at 05:25

0 Answers0