foo(int,int)被挑选在foo(int ...)上

发布于 2025-02-11 15:23:13 字数 394 浏览 1 评论 0 原文

在此代码中,为什么编译器无法引用从静态上下文中具有varargs参数的方法。

 private static void doSomething(int... nums) {
    System.out.println("1");
}
private void doSomething(int num1, int num2) {
    System.out.println("2");
} 

public static void main(String[] args) {
    doSomething(1,2);
}

JDK 17正在抱怨无法对非静态方法(int,int)进行静态引用。这是我不知道的错误还是另一个功能。

JDK 8和JDK 11不要抱怨它!

In this piece of code, why the compiler is not able to reference the method that has varargs argument from static context.

 private static void doSomething(int... nums) {
    System.out.println("1");
}
private void doSomething(int num1, int num2) {
    System.out.println("2");
} 

public static void main(String[] args) {
    doSomething(1,2);
}

JDK 17 is complaining Cannot make a static reference to the non-static method doSomething(int, int) . Is this a bug or another feature I am not aware of.

JDK 8 and JDK 11 dont complain about it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水波映月 2025-02-18 15:23:13

拒绝该方法调用的行为是正确的。方法分辨率应首先找到最特定的方法,然后检查以后 static 修饰符。

但是 javac 从版本7到16中都有一个错误。

请参见

jdk -8268474,方法分辨率应停止静态错误

Javac与JLS 16规范的第15.12.3节不同步。特别是,对于静态上下文中的调用,Javac过早地检查该方法是否是静态的,并且当它确定它不是静态并且无法调用的方法时,即使它应该报告编译时间错误。

该错误已用JDK  17修复。

请注意,Eclipse的编译器ECJ没有此错误,因此无论您使用哪种JDK版本,该示例在Eclipse中被拒绝。

仅使用简单名称的调用受到影响。当您使用 className.dosomething(1,2); 时,所有版本都将把调用解析为 dosomething(int num1,int num2)并生成正确的错误。

The behavior of rejecting the method invocation is correct. Method resolution should find the most specific method first and check for the static modifier afterwards.

But there is a bug in javac from version 7 to 16.

See

JDK-8268474, Method resolution should stop on static error

Javac is not in sync with section §15.12.3 of the JLS 16 specification. In particular, for invocations in static context, javac too early checks whether the method is static, and when it determines it is not static and cannot be called, proceeds to a further phase in method lookup, even though it should report a compile-time error.

This bug has been fixed with JDK 17.

Note that Eclipse’s compiler ECJ does not have this bug, so the example is rejected in Eclipse, regardless of which JDK version you use.

Only invocations using a simple name are affected. When you use ClassName.doSomething(1,2);, all versions will resolve the invocation to doSomething(int num1, int num2) and generate the correct error.

别在捏我脸啦 2025-02-18 15:23:13

使用两个INT的滴定方法是一种实例方法,您正在尝试不先创建对象实例的情况。

为什么编译器应该接受?我高度怀疑,在任何先前的JDK中,其行为都不同。

The doSomething method which takes two ints is an instance method and you are trying to call it without creating an instance of the object first.

Why should the compiler accept that? I highly doubt, that in any prior JDK it behaved differently.

爱你不解释 2025-02-18 15:23:13

一种静态方法只能调用其他静态方法。它无法调用非静态方法。 ,这就是为什么 non static dosomething 函数始终被调用的原因。

为了调用 dosomething(int,int),您必须将其制作 static 或创建包含该方法的类的对象,然后使用对象调用该方法。

YourClass obj = new YourClass();
obj.doSomething(1, 2);

A static method can call only other static methods; it cannot call a non-static method. So that's why the non-static doSomething function is always called.

In order to call the doSomething(int, int) you have to make it static or create an object of the class containing the method and then call the method using the object.

YourClass obj = new YourClass();
obj.doSomething(1, 2);
如痴如狂 2025-02-18 15:23:13

varargs是数组的替代。编译器正在给您错误,因为您以varargs不可接受的方式调用 dosomething

要调用varargs方法,您应该调用类似的内容:

doSomething(new int [] {1,2});

通过这种方式,您将正确调用>私有静态空白(int ... nums)

在JDK8(8U41)&在JDK17上。

此外,调用 dosomething(1,2)在JDK8& JDK17,所以我在两个版本上都没有发现任何不同的东西。

varargs are replacement for arrays. Compiler is giving you error because you are calling doSomething in a manner which is not acceptable for varargs:

To call your varargs method you should call something like:

doSomething(new int [] {1,2});

By this way, you will be correctly calling private static void doSomething(int... nums)

Tested this in JDK8 (8u41) & on JDK17.

Moreover, calling dosomething(1,2) is giving error on both JDK8 & JDK17 so I am not finding anything different on both versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文