Can we overload the methods by making them static?

DWQA QuestionsCategory: JavaCan we overload the methods by making them static?
Editor Staff asked 10 months ago

Can we overload the methods by making them static?

1 Answers
Editor Staff answered 10 months ago

No, We cannot overload the methods by just applying the static keyword to them(number of parameters and types are the same). Consider the following example.
public class Animal
{
void consume(int a)
{
System.out.println(a+” consumed!!”);
}
static void consume(int a)
{
System.out.println(“consumed static “+a);
}
public static void main (String args[])
{
Animal a = new Animal();
a.consume(10);
Animal.consume(20);
}
}
Output
Animal.java:7: error: method consume(int) is already defined in class Animal
static void consume(int a)
^
Animal.java:15: error: non-static method consume(int) cannot be referenced from a static context
Animal.consume(20);
^
2 errors