
Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
> > public static String method1( Object obj ){
> > return method1( new Object[]{ obj } );
[quoted text clipped - 5 lines]
>
> Just what sort of complaint were you expecting the compiler to make?
Because I think of another scenario like below. There are two methods of the
same name but different signature.
public static String method1( Object obj ){
............
}
public static String method1( Object[] obj ){
............
}
For example if I were to execute
method1(new Object[]{"1", "2"});
method1( Object obj ) get called if I forget to define method1( Object[]
obj )
That is why I expect the compiler to give a warning or error when the
example above happens.
Thomas Schodt - 28 Sep 2005 09:14 GMT
> Because I think of another scenario like below. There are two methods of the
> same name but different signature.
[quoted text clipped - 14 lines]
> That is why I expect the compiler to give a warning or error when the
> example above happens.
Maybe you want something like
public static String method1(Object obj){
if (obj==null) return "";
return obj.getClass().isArray()?method1a(obj):method1o(obj);
}
private static String method1a(Object[] obj){
...
}
private static String method1o(Object obj){
...
}
James Yong - 28 Sep 2005 14:12 GMT
> > Because I think of another scenario like below. There are two methods of the
> > same name but different signature.
[quoted text clipped - 27 lines]
> ...
> }
Hi Thomas,
hmm..I never thought of this approach before.
obj.getClass().isArray() is new to me. haha. thanks
Regards,
James
Roedy Green - 28 Sep 2005 09:52 GMT
> public static String method1( Object obj ){
> ............
[quoted text clipped - 3 lines]
> ............
> }
In this case the compiler does a best fit. It looks for the most
specific fit. So String[] goes with the second but String goes with
the first

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
James Yong - 28 Sep 2005 14:05 GMT
> > public static String method1( Object obj ){
> > ............
[quoted text clipped - 7 lines]
> specific fit. So String[] goes with the second but String goes with
> the first
Hi Roedy,
Thanks for the explanation.
Regards,
James
Roedy Green - 28 Sep 2005 09:56 GMT
>For example if I were to execute
> method1(new Object[]{"1", "2"});
[quoted text clipped - 3 lines]
>That is why I expect the compiler to give a warning or error when the
>example above happens.
If it did that, to be consistent, any method( Object) would generate a
warning every time any parameter were used but Object. Taking that
further, every method (Interfacexx ) would generate a warning. Every
time you used any subclass of the class defined in the method as a
parameter you would get a warning. The compiler would be crying wolf.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.