Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor Confusion

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the parameter list in that one is a primitive.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or more to the point, because "null" isn't a valid boolean value.
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Amy Medrat
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Vinayak , i think i got the point
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Amy , u really explained everything
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, in short: arrays of primitives are objects.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic