Method overloading with both static and non-stationary methods

As I found out, when overloading a Java method, we use the same name for all overloaded methods. And also their return type is not a question. But what happens if we use the same method as static and non-static, as in the example below? Can this method be considered overload?

class Adder {

    static int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }

}

class Test {

    public static void main(String[] args) {
        Adder a1 = new Adder();

        System.out.println(Adder.add(11, 11));

        System.out.println(a1.add(11, 11, 51));

    }
}

I read several articles, but they did not clarify my question.

+4
source share
2 answers

Using a keyword staticdoes not affect method overloading.

Your code compiles because the method signature of both methods add()is different (2 parameters compared to 3 parameters).

, - , .

class Adder {
    static int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b) {
        return a + b + c;
    }
}
+1

, . JLS:

( ) , , , .

.

0

Source: https://habr.com/ru/post/1673213/


All Articles