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");
}
}
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");
}
}
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("");
^
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.