aspose file tools*
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
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: 7639
    
  19

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: 38340
    
  23
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: 605

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