aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes varargs - method resolution principle??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "varargs - method resolution principle???" Watch "varargs - method resolution principle???" New topic
Author

varargs - method resolution principle???

Biju Joseph
Greenhorn

Joined: Jul 30, 2003
Posts: 5
Can someone please explain the resolution rule, based on the program below...

---- Output -----------------

---------------------------------------------------
At lines marked 1 and 2 , I created object of B and assigned it in variables of type B and A respectively.
At lines 3 and 7, call is made on an actual instance of B, produce different results why so?
Similarly line 5 and 9, is giving different result.
Line 6 is giving a compile time error though the same call at line 10 is working fine..
Similarly line 8 is throws compile time error, where as the same call works at line 4

Someone please explain me why the calls gets routed to different methods when only the
type of the variable holding actual reference is different.?
[ July 26, 2005: Message edited by: Barry Gaunt ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Actually, i was jsut reading on this.

1. Make sure old code does not break, so therefore, when you have these choices, this is the order

1. Exact match.
1B. Method with a Parent Class as the parameter.
2. widening of primitives.
3. Autoboxing and Unboxing
4. Varargs

Hope that helps, and that I got it right.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Sun gives some advice:
Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Biju,

Which specific method to select, and under what situation, ambiguity arises are stated in JLS3 15.12.12 Compile-Time Step2: Determine Method Signature.

At lines 3 and 7, call is made on an actual instance of B, produce different results why so?

Hmmm...I was wondering how did you manage to get line 7 compiled without error?

Joyce
[ July 26, 2005: Message edited by: Joyce Lee ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
The question is, "which specification are you following?". Given that this part of the specification has jumped about more times than I've consumed beers (ok, maybe an exaggeration), the answer is flaky at best.

Here are related articles:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6249122
http://forum.jtiger.org/posts/list/24.page

You'll note that not even Sun know what they are doing with the specification in this regard (have you attempted to decipher the existing 3rd edition spec. yet?). You can bet (hope?) that Sun aren't going to put a question on the SCJP exam, that they clearly cannot answer themselves.


Tony Morris
Java Q&A (FAQ, Trivia)
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Biju, which compiler's version "javac -version" are you using?

[Tony]: Given that this part of the specification has jumped about more times than I've consumed beers (ok, maybe an exaggeration), the answer is flaky at best.

Hey, how often do you drink beers?

Here are related articles:

Thanks for the articles, Tony.
[ July 26, 2005: Message edited by: Joyce Lee ]
Biju Joseph
Greenhorn

Joined: Jul 30, 2003
Posts: 5
Thank you Tony, Mark , Barry, and Joyce....
Joyce, I use JDK version 1.5.0_04. I am using Eclipse 3.1.0 (Build id: 200412162000)IDE.
As you pointed out, Line 7 and 9 throw compile time error, when used the javac at command line. But it�s compiling well in Eclipse, which in turn points to the same JRE.
Hope these kind of confusing questions wont be asked in exam�
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
[Biju]:As you pointed out, Line 7 and 9 throw compile time error, when used the javac at command line. But it�s compiling well in Eclipse, which in turn points to the same JRE.

The compiler used by Eclipse is not the same as the Sun JDK's compiler. My suggestion is to use Sun JDK's compiler when experimenting with the code for SCJP.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: varargs - method resolution principle???