为什么javac会发出“错误:类中的方法不能应用于给定类型"?在静态上下文中使用实例方法时? [英] Why does javac emit "error: method in class cannot be applied to given types" when an instance method is used in a static context?
本文介绍了为什么javac会发出“错误:类中的方法不能应用于给定类型"?在静态上下文中使用实例方法时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下(无效的)Java 程序:
Consider the following (invalid) Java program:
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);
}
}
}
我预计会出现与此类似的错误:
I would expect an error similar to this:
Cannot make a static reference to the non-static method print(int[]) from the type Test
at Test.main(Test.java:5)
相反,javac 发出:
but instead, javac emits:
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_11
java -version
javac -version javac 1.8.0_11
java -version
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)
这是在这个问题中出现的.这是javac中的错误吗?还是我在这里遗漏了一些明显的东西?
This came about in this question. Is this a bug in javac? Or am I missing something dead obvious here?
推荐答案
这是一个错误,已报告为 JDK-8055514.(你?)
This is a bug and has been reported as JDK-8055514. (By you?)
这篇关于为什么javac会发出“错误:类中的方法不能应用于给定类型"?在静态上下文中使用实例方法时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文