Which method is looked first by Compiler , Static or instance method when ClassName.method() is used?

前端 未结 4 1321
忘了有多久
忘了有多久 2021-01-16 06:44

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

4条回答
  •  孤独总比滥情好
    2021-01-16 07:15

    You have no return type here:

    public static xyz(Integer i) {
    }
    

    This should be void, if there is nothing to return:

    public static void xyz(Integer i) {
    }
    

    And also, you need to make the first method static too:

    public static void xyz(int i) {
    }
    

    So it can be called out of the static main method. It is not possible to call non-static methods out of static methods. More detailed explanation on this: calling non-static method in static method in Java

提交回复
热议问题