wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Constructor Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor Confusion" Watch "Constructor Confusion" New topic
Author

Constructor Confusion

faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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....
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
When a method or constructor is called, the compiler tries to match the signature which is most specific to the method or constructor call.
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Because the parameter list in that one is a primitive.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Or more to the point, because "null" isn't a valid boolean value.


[Jess in Action][AskingGoodQuestions]
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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
Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
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 ]

"I can resist everything except temptation"
Amy Medrat
Greenhorn

Joined: Mar 05, 2006
Posts: 15
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 ]

medRat
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
Thanx Vinayak , i think i got the point
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
Thanx Amy , u really explained everything
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Or, in short: arrays of primitives are objects.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor Confusion