มีความคลุมเครือในขณะที่ใช้อาร์กิวเมนต์ตัวแปรใน Java สิ่งนี้เกิดขึ้นเพราะสองวิธีสามารถถูกต้องเพียงพอที่จะเรียกโดยค่าข้อมูล ด้วยเหตุนี้คอมไพเลอร์จึงไม่มีความรู้ว่าจะเรียกวิธีใด
ตัวอย่าง
public class Demo {
static void my_fun(double ... my_Val){
System.out.print("fun(double ...): " + "Number of args: " + my_Val.length );
for(double x : my_Val)
System.out.print(x + " ");
System.out.println();
}
static void my_fun(boolean ... my_Val){
System.out.print("fun(boolean ...) " + "The number of arguments: " + my_Val.length);
for(boolean x : my_Val)
System.out.print(x + " ");
System.out.println();
}
public static void main(String args[]){
my_fun(11.56, 34.78, 99.09, 56.66);
System.out.println("Function 1 has been successfully called");
my_fun(true, false, true, false);
System.out.println("Function 2 has been successfully called");
my_fun();
System.out.println("Function 3 has been successfully called");
}
} ผลลัพธ์
Demo.java:23: error: reference to my_fun is ambiguous my_fun(); ^ both method my_fun(double...) in Demo and method my_fun(boolean...) in Demo match 1 error
คลาสชื่อ Demo กำหนดฟังก์ชันชื่อ 'my_fun' ที่ใช้จำนวนตัวแปรของค่าจุดลอยตัว ค่าจะถูกพิมพ์บนคอนโซลโดยใช้ลูป 'for' ฟังก์ชันนี้มีการใช้งานมากเกินไป และพารามิเตอร์เป็นค่าบูลีนที่มีจำนวนแตกต่างกัน เอาต์พุตจะแสดงบนคอนโซลโดยใช้ลูป 'for'
ในฟังก์ชันหลัก ขั้นแรกเรียก 'my_fun' ด้วยค่า ppoint แบบลอยตัว จากนั้นด้วยค่าบูลีน และไม่มีพารามิเตอร์ใดๆ ข้อยกเว้นที่ส่งผลคือแสดงบนคอนโซล