my dog learned polymorphism
The moose likes Java in General and the fly likes Overloading constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overloading constructors" Watch "Overloading constructors" New topic

Overloading constructors

Dirk Neefs

Joined: Apr 10, 2003
Posts: 1
I have a problem when invoking overloaded constructors when I pass null as a parameter. This is my code:

When I call isString() it returns false.
My question now is: what are the rules to invoke an overloaded constructor and what is the impact on this for the null value?
[ May 22, 2003: Message edited by: Cindy Glass ]
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Hi, Dirk, and Welcome to JavaRanch.
In general, answer to your question lies in the fact that null is considered to be a very specific value, and when faced with overloaded methods, the most specific one will be called.
(My examples below are methods, but I believe that the same holds true for constructors)
That is, if you had:

and called aMethod(null), it would print "Integer" since that is the most specific method. However, if you also had

and called aMethod(null), you would get a compile-time error, since the compiler cannot tell what you class you mean for null to be (Integer and String are of equal specific-ness). You would have to cast your null, i.e., aMethod((String)null).
However, when I try to compile you code, I get the following errors:

Of course this is because byte[] and char[] are not Strings in Java.
[ May 22, 2003: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: Overloading constructors
It's not a secret anymore!