File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Var-Args problem 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 "Var-Args problem" Watch "Var-Args problem" New topic

Var-Args problem

garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Source: and Practice Exam: 2 Question No:33

If I uncomment the above commented lines, code does not compile. With comments, it compiles just fine. That goes to say that overloading is fine here. My question is whether there is any way to call any of these two methods successfully.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Overloading, compilation wise, is fine here. But usage wise, its not fine. Because, at the end, you are unable to call these methods because of the ambiguity issue, as there is varargs in one call and the other is boxing+varargs...

One way of solving this issue would be to change the first callMethod to public static void callMethod(Integer i){ (removal of varargs syntax)...that way, you will get Primitive as the output.

Another option is to remove the varargs from the second call. Or else, in the parameters you are passing, do not pass variables that involve unboxing...

Solutions are many...But, with the same exact code you have given, I don't think you can call those methods.
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Thanks Rekha...
Fabio Nascimento

Joined: Nov 16, 2008
Posts: 16
I didnt know about that ambiguous error, what a tricky for the exam heh
I agree. Here's the link:
subject: Var-Args problem
It's not a secret anymore!