当使用ClassName.method()时,哪个方法首先由编译器,静态或实例方法查看? [英] Which method is looked first by Compiler , Static or instance method when ClassName.method() is used?

查看:231
本文介绍了当使用ClassName.method()时,哪个方法首先由编译器,静态或实例方法查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想正确理解为什么下面的编译错误?
根据我的理解如果我使用Test.xyz()然后编译器只寻找静态方法不为实例方法然后为什么以下编译失败?

I want to get proper understanding why below compilation error? As per my understanding If i use Test.xyz() then compiler look for only static method not for instance method then why below compilation fail?

class Test {
    public static void main(String arg[]) {
        Test.xyz(10);     // compilation fail
    }   

    public void xyz(int i) {
    }
    public static void xyz(Integer i) {
    }   
 }

每一个都请建议编译失败而不是其他建议以及如何使用,

Every one please suggest why compilation fail rather than other suggestions and how to use , I know all basic thing Autoboxing etc .

推荐答案

为什么会出现编译错误



编译通过不同的步骤进行。从JLS提取,以下是解释为什么会出现此错误的规则。

Why there is a compilation error

Compilation proceeds through different steps. Extracted from the JLS, following are the rules that explain why you got this error.

我跳过第一步,这与您的案例无关。一切都发生在同一个类中。

I skip the first step which is not relevant in your case. Everything happens in the same class.

第二步:确定方法签名


多于一种这样的方法,在这种情况下选择最具体的一种。最具体方法的描述符(签名加返回类型)是在运行时用于执行方法分派的

There may be more than one such method, in which case the most specific one is chosen. The descriptor (signature plus return type) of the most specific method is one used at run time to perform the method dispatch

第一阶段(§15.12.2.2)分辨率,不允许加载或取消加载转换,或使用变量arity方法调用。如果在此阶段没有找到适用的方法,则处理继续到第二阶段。

The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

从上面的注释, code> Test.xyz(10); 是需要一个 int 参数的方法:

From above comments, the method you invoke with Test.xyz(10); is the one that takes an int parameter:

public void xyz(int i) {}

但现在,还有第三步:选择适当的方法

But now, there is a third step: Choosing the appropriate method


如果方法调用在左括号之前有一个MethodName形式 TypeName。标识符,或者如果方法调用在左括号之前具有TypeName形式。 NonWildTypeArguments标识符,则编译时声明必须为static ,否则会发生编译时错误。

If the method invocation has, before the left parenthesis, a MethodName of the form TypeName . Identifier, or if the method invocation, before the left parenthesis, has the form TypeName . NonWildTypeArguments Identifier, then the compile-time declaration must be static, or a compile-time error occurs.

再次,从上面的注释,你调用 static 表单中的方法,

Again, from the above comment, you invoke the method in a static form,

Test.xyz(10);

但不幸的是,从第二步选择的方法是 static 。

but unfortunately, the method chosen from the second step, is not static.

这就是为什么像Eclipse这样的IDE建议将'xyz()'更改为静态。

It's why an IDE like Eclipse will suggest to "Change 'xyz()' to static".

但是如我在第一个答案(删除)中所解释的,您可以调用 public void xyz(int i){} Test 的实例上调用 static / code>参数: Test.xyz(Integer.valueOf(10));

But as explained in my first answer (deleted), you can either call public void xyz(int i) {} on an instance of class Test, or call the static method with an Integer parameter: Test.xyz(Integer.valueOf(10));.

两者都可以工作。

这篇关于当使用ClassName.method()时,哪个方法首先由编译器,静态或实例方法查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆