They are static and non-stationary overloads of each other

Are these two functions overloads

class yogi{

   public static void fun(){
    System.out.println("Fun");
   }    

   public void fun(int a,int b){
    System.out.println("int");
   }

}
+3
source share
2 answers

Yes, this is overload. From JLS 8.4.9 :

If two methods of a class (both declared in the same class or both inherited by the class or one declared and one inherited) have the same name, but signatures that are not equivalent for overriding, then the method name said that it was overloaded.

It’s pretty rare (IMO) that it is a good idea to have the same name for both static and instance, but that’s completely true.

, , , . :

public class Test {
    public void foo(String x) {
    }

    public static void foo(Object x) {
    }

    public static void bar() {
        foo(""); // Error
    }
}

, ( ), :

Test.java:9: error: non-static method foo(String) cannot be referenced
                    from a static context
        foo("");
        ^
+7

, . "fun".



" .

:
1. .
2. . , .

, , : Duplicate method fun (int, int) ...

public static void fun(int a, int b) {
    System.out.println("int");
}

public void fun(int a, int b) {
    System.out.println("int");
}

, . .

0

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


All Articles