Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Overloaded method resolution

802205Oct 6 2010 — edited Oct 6 2010
Please tell me if my understanding is correct:

If a method is overloaded, which method to call is resolved at compile time, and the compiler will select the narrowest definition that matches the declared type of the arguments. In other words, if I have these methods:
void foo(Object obj) { ... }
void foo(String str) { ... }
And I declare a String reference and call foo with it, it will use foo(String). However, if I declare an Object reference, assign a String to it, and call foo, it will use foo(Object) because it uses the reference type, not the object type. Correct?

Really what I'm asking is if this is guaranteed, because I could just test it...
This post has been answered by JoachimSauer on Oct 6 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 3 2010
Added on Oct 6 2010
4 comments
1,491 views