0

In Bukkit's API, there are two functions that show up as ambiguous when I try to call them

int getMaxHealth()
double getMaxHealth()

I can't figure out how to specify to the compiler which function I want to call. Instead of directly comparing like

if (getMaxHealth() < 40d) { /* Code */ }

I tried "directly" assigning the variable

double health = getMaxHealth();

However, both cases result in IntelliJ complaining that it is an ambiguous method. How can I fix this?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
dreadiscool
  • 1,598
  • 3
  • 18
  • 31
  • maybe you get a jar with error, where is the jar location? – Loki Aug 29 '13 at 07:39
  • It's not an error, both methods exist to provide backwards compatibility to older plugins – dreadiscool Aug 29 '13 at 07:40
  • Which class or interface do these methods belong to ? I am taking a look at the [Bukkit API](http://jd.bukkit.org/rb/apidocs/) and i can only find the interface `Damageable` which has a method `int getMaxHealth()` – jeroen_de_schutter Aug 29 '13 at 08:07

4 Answers4

1

IntelliJ often can help you solve the problem. Put your cursor on the code which is marked incorrect and then press Alt+Enter (if you have the Windows version, don't know the key combination on other OS ...). The IDE might suggest a solution to you.

jeroen_de_schutter
  • 1,843
  • 1
  • 18
  • 21
1

Overloaded methods are determined with their parameter list. Changing return type does not change the signature of method from compiler's view-point. So Java's compiler is complaining about redefining same method which is not allowed and it has nothing to do with IntelliJ or Eclipse.

Ean V
  • 5,091
  • 5
  • 31
  • 39
  • You should either change the name of the method or pass a parameter. You can't have two methods with same name in one class. I mean something like: `getMaxHealthInt()` and `getMaxHealthDouble()` or `int getMaxHealth(int input)` and `double getMaxHealth(double intput)` – Ean V Aug 29 '13 at 09:07
  • If all you need it to get a max health value, why you don't use int version always? JVM will implicitly convert it to double where is needed. – Ean V Aug 29 '13 at 09:14
1

Apparently, I can still compile the program even though there is an error. So I guess that fixes the problem!

dreadiscool
  • 1,598
  • 3
  • 18
  • 31
0

I don't think you can fix this.

If I write, using Eclipse, code as yours:

int getMaxHealth() {return 1;}
double getMaxHealth() {return 1d;}

It even does not compile. Both methods are marked as duplicates.

dantuch
  • 9,123
  • 6
  • 45
  • 68