| Author |
Varargs in java 6 having some problem
|
sureshkumar settu
Greenhorn
Joined: May 05, 2010
Posts: 14
|
|
Hi guys,
I an working in JAVA 6. We i do overloading a method with varargs, Only var args method is calling.
i.e.
I have two method
public Map<String> getData(String... field){}
public Map<String> getData(String field){}
i am calling like getData("suresh")
Always it uses varargs method i.e first method. Why i am not able to call second one?
Thanks,
suresh kumar
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4725
|
|
sureshkumar settu wrote:i am calling like getData("suresh")
Always it uses varargs method i.e first method. Why i am not able to call second one?
Seems odd to me. However, what you could try is swapping the methods around (ie, put the single String method first); I'm pretty certain that when the compiler can't make any clear choice it uses the definition order.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
That looks the wrong way round; for a single argument you ought to execute the non-varargs version of those methods.
|
 |
Saifuddin Merchant
Ranch Hand
Joined: Feb 08, 2009
Posts: 576
|
|
Campbell Ritchie wrote:That looks the wrong way round; for a single argument you ought to execute the non-varargs version of those methods.
Surprised me too. So I tried it out quickly
Output
Normal
Working as expected!
|
Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
|
 |
sureshkumar settu
Greenhorn
Joined: May 05, 2010
Posts: 14
|
|
Thanks for the reply guys.
I tried whole day, that was not working on that day.
but today when i pass single argument then it is calling single argument version of method. I don't know how it is working now, nothing i have changed in my coding.
That class may not be compiled correctly. I don't know the exact reason.
what ever guys now i understand. JVM always prefer earlier version of calling mechanism.
thanks
suresh
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
|
You probably had old *.class files that were being picked up.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Varargs in java 6 having some problem
|
|
|