FIX: Jvc.exe Gives Ambiguity Error on Overloaded MethodsID: Q177166
|
When using the new Java compiler provided in the Microsoft SDK for Java 2.0 (Jvc.exe version 1.02.4337), compiling code with overloaded methods whose parameters are various combinations of double and long types gives the following error:
Ambig.java(14,5) : error J0079: Ambiguity between
'void Ambig.foo(double d, long l)' and 'void Ambig.foo(long l, double
d)'
A bug in the Java Compiler (Jvc.exe) produces this error when the overloaded method that takes two doubles appears in the code first and directly before the method that takes two longs.
To work around this problem, either move the method that takes two doubles to any point in the class after the method that takes two longs, or move the method that takes two longs anywhere in the code after the other overloaded methods.
Microsoft has confirmed this to be a bug in the Microsoft products listed
at the beginning of this article.
This bug was corrected in Microsoft virtual machine contained in the SDK for Java 3.0 and later.
To reproduce the problem, compile the following code:
public class Ambig
{
public static void main(String args[])
{
Ambig b = new Ambig();
b.foo((long)1, (double)2);
b.foo((double)1, (long)2);
b.foo(1,2);
b.foo(1.5,2.5);
// pause for user to press return
try{System.in.read();}catch(Exception e){}
}
public void foo(double d, double d2)
{
System.out.println("Double = "+d+" Double2 = "+d2);
}
public void foo(long l, long l2)
{
System.out.println("Long = "+l+" Long2 = "+l2);
}
public void foo(long l, double d)
{
System.out.println("Long = "+l+" Double = "+d);
}
public void foo(double d, long l)
{
System.out.println("Double = "+d+" Long = "+l);
}
}
You should see the following error:
Ambig.java(14,5) : error J0079: Ambiguity between
'void Ambig.foo(double d, long l)' and 'void Ambig.foo(long l, double
d)'
To work around this problem, change the order of the overloaded methods to
the following:
public void foo(long l, long l2)
{
System.out.println("Long = "+l+" Long2 = "+l2);
}
public void foo(long l, double d)
{
System.out.println("Long = "+l+" Double = "+d);
}
public void foo(double d, long l)
{
System.out.println("Double = "+d+" Long = "+l);
}
public void foo(double d, double d2)
{
System.out.println("Double = "+d+" Double2 = "+d2);
}
-or-
public void foo(double d, double d2)
{
System.out.println("Double = "+d+" Double2 = "+d2);
}
public void foo(long l, double d)
{
System.out.println("Long = "+l+" Double = "+d);
}
public void foo(double d, long l)
{
System.out.println("Double = "+d+" Long = "+l);
}
public void foo(long l, long l2)
{
System.out.println("Long = "+l+" Long2 = "+l2);
}
For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:
http://support.microsoft.com/support/visualj/
http://support.microsoft.com/support/java/
Additional query words:
Keywords : kberrmsg kbnokeyword kbSDKJava300fix kbVJ kbSDKJava310fix
Version : WINDOWS:1.0,1.1,2.0,2.01,2.02
Platform : WINDOWS
Issue type : kbbug
Last Reviewed: July 26, 1999