This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Varargs in java 6 having some problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Varargs in java 6 having some problem" Watch "Varargs in java 6 having some problem" New topic
Author

Varargs in java 6 having some problem

sureshkumar settu
Greenhorn

Joined: May 05, 2010
Posts: 16

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: 7526
    
  18

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?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
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: 597

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: 16

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Varargs in java 6 having some problem
 
Similar Threads
determine the input type of the value of the function parameter map
Not just another Primitive Data Type Question ? Why Priimitive ? Varargs
How to set a time limit on a java function running a regex
Object invoke (Reflection)
Question on resolving generics