4

Does these two function overloads

class yogi{

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

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

}
dead programmer
  • 4,223
  • 9
  • 46
  • 77
  • You can't have overloads like this. They're called completely differently. `yogi.fun()` and `myYogiInstance.fun()`. – Phylogenesis Apr 11 '15 at 18:27
  • 1
    @Phylogenesis: You absolutely *can* have overloads like that. What error do you expect to see? Just because they're called differently doesn't mean they're not overloads. – Jon Skeet Apr 11 '15 at 18:27
  • I wouldn't call it method overloading. It's the nomenclature I have issues with. – Phylogenesis Apr 11 '15 at 18:28
  • @Phylogenesis: Then you should take it up with the spec :) – Jon Skeet Apr 11 '15 at 18:28
  • @JonSkeet Perhaps I should :) Personally, I see similarly named static and instance methods as having as much in common as `myClass1.function()` and `myClass2.function()`. – Phylogenesis Apr 11 '15 at 18:29
  • @Phylogenesis: See my answer for an example of an instance method participating (unhelpfully) in overload resolution even in a static context. So it seems entirely reasonable to call this overloading. – Jon Skeet Apr 11 '15 at 18:33
  • @JonSkeet You learn something every day. Didn't know that Java's overload resolution made instance methods available in a static context. – Phylogenesis Apr 11 '15 at 18:35
  • 1
    @Phylogenesis: Me either :) (I knew about the reverse, which is annoying. I hate being able to call static methods "via" instances...) – Jon Skeet Apr 11 '15 at 18:36

2 Answers2

8

Yes, those are overloads. From JLS 8.4.9:

If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

It's fairly rare (IMO) for it to be a good idea to have the same name for both static and instance methods, but it's entirely valid.

Interestingly, this can cause problems in overload resolution, in that instance methods are included even when there's no instance to call the method on. For example:

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

    public static void foo(Object x) {
    }

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

The spec could have been designed so that this would be fine (and call the static method) but instead it's an error:

Test.java:9: error: non-static method foo(String) cannot be referenced
                    from a static context
        foo("");
        ^
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

I think YES. They overload the method name "fun".

Some good points about overloading can be found here

"Method overloading is also known as Static Polymorphism.

Points to Note:
1. Static Polymorphism is also known as compile time binding or early binding.
2. Static binding happens at compile time. Method overloading is an example of static binding where binding of method call to its definition happens at Compile time."

The following code for example gives compile time error: Duplicate method fun(int, int) in type ...

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

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

From compile time binding point of view the instance methods and static methods cannot be separated because static methods can also be called on the instance variables not only on the class references. And the compiler has to resolve in unambiguously.

Laszlo Hirdi
  • 1,140
  • 12
  • 18