Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Varargs in java 6 having some problem

 
sureshkumar settu
Greenhorn
Posts: 16
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50251
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
sureshkumar settu
Greenhorn
Posts: 16
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably had old *.class files that were being picked up.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic