gpt4 book ai didi

java - 为什么在静态上下文中使用实例方法时 javac 会发出 "error: method in class cannot be applied to given types"?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:27:37 26 4
gpt4 key购买 nike

考虑以下(无效的)Java 程序:

public class Test {
public static void main(String[] args) {
int[] ints = {1, 2, 3, 4, 5};
print(ints);
}

public void print(int... ints) {
for (int i : ints) {
System.out.print(i);
}
}
}

我希望出现与此类似的错误:

Cannot make a static reference to the non-static method print(int[]) from the type Test
at Test.main(Test.java:5)

相反,javac 发出:

Test.java:5: error: method print in class Test cannot be applied to given types;
print(ints);
^
required: int[]
found: int[]
reason: varargs mismatch; int[] cannot be converted to int
1 error

javac -version javac 1.8.0_11java-版本

java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

这发生在 this question .这是 javac 中的错误吗?还是我在这里遗漏了一些明显的东西?

最佳答案

这是一个错误,已报告为 JDK-8055514 . (由你?)

关于java - 为什么在静态上下文中使用实例方法时 javac 会发出 "error: method in class cannot be applied to given types"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25397551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com