Forums Register Login

Constructor Confusion

+Pie Number of slices to send: Send
Hello ranchers ,

This code was actually pasted on the "Thread and Synchronization" block , here is the code

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class Confusing {

private Confusing()
{
System.out.println("Zero Arg-Constructor");
}

private Confusing(Object o) {
System.out.println("Object....");
}

private Confusing(boolean Array[]) {
System.out.println("Boolean...");
}

public Confusing(double a)
{
System.out.println("Public constructor.");
}




static public final void main(final String[] notUsed)
{
new Confusing(null);
}
}

On executing the above code it gives

Output
Boolean...

why the 'private Confusing(boolean []Array)' constructor is getting called for 'null' , one last thing if i change the signature of constructor to

private Confusing(boolean Array) // change the array to a boolean variable '

output is
Object....
+Pie Number of slices to send: Send
When a method or constructor is called, the compiler tries to match the signature which is most specific to the method or constructor call.
+Pie Number of slices to send: Send
I agree with you , but when i change the signature of the constructor to

private Confusing(boolean Array)

it is calling constructor

private Confusing(Object o)

as far as i know null is a special literal rather than an object , then why is it calling this constructor and not the constructor "private Confusing(boolean Array)"


Hope you got my point

regards
+Pie Number of slices to send: Send
Because the parameter list in that one is a primitive.
+Pie Number of slices to send: Send
Or more to the point, because "null" isn't a valid boolean value.
+Pie Number of slices to send: Send
 

Originally posted by Ernest Friedman-Hill:
Or more to the point, because "null" isn't a valid boolean value.



Ok sir, I agree with you but when i make constructor

private Confusing(boolean Array[])
rather than this

private Confusing(boolean Array)
it is giving output as
Boolean ...

when the object is instantiated
----------------------------------------------------------------------------------------------------------------------

Here in this case also , "null is not a valid Boolean value " then why this constructor[ private Confusing(boolean Array[]) ] is getting called ?

Sorry , i am not getting things correctly

as soon as i make an "array of boolean variables" in the Constructor signature , it gives me output as

Boolean ...

but when i change the signature of the Constructor to a boolean variable like this

private Confusing(boolean Array)
it gives me output as

Object ...
"Though in both the cases null is not a valid value for a boolean ."

Please help me .... :roll:

If possible do explain this also

When i make a constructor having an array of Boolean the output is

Boolean...

but when i make a constructor having a variable of Boolean type the output is

Object.... ? ?



Thanx in advance
+Pie Number of slices to send: Send
Hi,

As far as I know, boolean Array[] is an array of boolean variables.
The constructor in this case takes a reference to an array of type boolean. A reference can be null So null matches it.
It is not the same as boolean where it expects a boolean value.

Hope I have not confused (pun unintended ) things further

-Vinayak
[ March 08, 2006: Message edited by: Vinayak patil ]
+Pie Number of slices to send: Send
Dear faisal,

First thing that you should understand, you cannot assign a null to a primitive data type, i.e. if I say
boolean bolVal = null;
or
int intVal = null;

is illegal.

A null value can be assigned to only object type.

So when you call �new Confusing(null);�

It searches for the constructor which have Object type in the parameter.

In the code above we only have two best fitted constructor here

1.private Confusing(Object o)
2. private Confusing(boolean Array[])

now out of these two private Confusing(boolean Array[]) is selected because Object is the super class of the Array object and it is the first to be best fitted.

Now if you change private Confusing(boolean Array[]) to private Confusing(boolean Array) it again becomes a primitive value so a null value cannot fit in this so after this change you get a out put as �Object�..�

Now lets say I add another constructor to the code you have above :
private Confusing(String o) {
System.out.println("String....");
}

the moment you also add this constructor to this you wont be able to compile the code as the it creates an ambiguity in which you cannot decide as to which constructor is best fitted Confusing(String o) OR Confusing(boolean Array[])

hope this has helped you in understanding the concept.
[ March 08, 2006: Message edited by: Amy Medrat ]
+Pie Number of slices to send: Send
Thanx Vinayak , i think i got the point
+Pie Number of slices to send: Send
Thanx Amy , u really explained everything
+Pie Number of slices to send: Send
Or, in short: arrays of primitives are objects.
They weren't very bright, but they were very, very big. Ad contrast:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 977 times.
Similar Threads
doubts in 4 questions
Overloading Puzzled
How does this output comes?
Wrapper Class equlas()
Boolean: valueOf() and equality doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 03:10:12.