43

I'm trying to access a method in a class I made, but since it is similar in name and in number of arguments my IDE says the method is ambiguous. Here's a mock-up of what the two methods look like:

methodName(X, Y, Z)
methodName(A, Y, Z)

I called on the method, and passed in the value null for the first argument for the purpose of my test. Unfortunately I cannot rename the methods, change the order of the arguments or modify the structure of the method in any way. Is there a way I can differentiate between these two methods?

Anil Bharadia
  • 2,760
  • 6
  • 34
  • 46
tamuren
  • 1,072
  • 4
  • 15
  • 32
  • 37
    Find out who designed that API, learn where they live and burn their house down. – alexg May 03 '12 at 20:28
  • 1
    @alexg I seem to remember hitting this before, although I forget which API; it's unlikely to have been new Thread(null, "name") for instance. – Neil May 03 '12 at 20:46
  • 7
    @alexg Since the question said "I'm trying to access a method in a class I made" user1373493 could burn their own house down or redesign the API. – emory May 04 '12 at 00:30
  • @alexg overloading is actually quite common and not necessarily a big problem in Java. This sort of issue only arises with `null` which doesn't have a type (and autoboxing in some circumstances). – Bruno May 04 '12 at 00:58
  • @Bruno, it would also arise for every type `T implements X, A` – emory May 04 '12 at 04:39
  • See http://stackoverflow.com/a/5229890/40342 for an in-depth description of why this issue happens. – Joachim Sauer May 04 '12 at 10:15
  • You could consider making a Facade Interface to the library, and call via your Facade instead http://en.wikipedia.org/wiki/Facade_pattern – Cheekysoft May 04 '12 at 14:10

2 Answers2

89

Cast the first argument to the type of the first parameter of the method you want to call, for example:

methodName((A) null, y, z);
fivedigit
  • 18,464
  • 6
  • 54
  • 58
0

Can you use reflection on the object to acquire the method-list? If so, you could then invoke explicitly the method relevant to you. I do believe fivedigit's answer is probably better though ...

Everyone
  • 2,366
  • 2
  • 26
  • 39