I have two methods in a class...
public void method(String var1, String var2, Object var3) {
//do stuff
}
public void method(String var1, String var2, Object[] var3) {
//do stuff
}
When I make the following call... obj.method("string", "string", obj)
the correct method is called, however, when I try to call obj.method("string", "string", obj[])
the first incarnation of that method is called.
Is there any annotation or "hint" I can give to make sure the method I anticipate being called will be called? I know an Object[]
is anObject
, but I would hope that at runtime the method with Object[]
would be called before Object
.
Nope, for me this calls the second method as expected!
In the case where you want one to be called over the other, then the hint you'd give is usually a cast to the exact parameter type expected by the method (this cast will work even on null
.) So using (Object)arr
would force the first method to be called, but by default it will definitely call the second method (on all correct versions of the JVM anyway - contrary to popular belief, this scenario is covered explicitly in the JLS and thus is not ambiguous.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With