Overloading
static method In Java
Yes, we can overload static method in Java. In terms of method
overloading static method is just like normal methods and in order to
overload the static method you need to provide another static method with the same name but
different method signature. Static overloaded method is resolved using Static
Binding during compile time. Overloading method in Java is completely
different than the overriding method and as discussed in the last article we can
not override static method in Java but we can certainly overload a static
method in Java. Here is an example which confirms that we can overload static method in Java:
Overloading Static method in Java - example

/**
* Java program to show that we can overload static method in Java.
*/
public class StaticOverloadingTest {
public static void main(String args[]) {
greet("John"); //will call a static method with one String argument
greet("John", "Good Morning"); //overloaded static method will be call
}
/*
* static method which will be overloaded
*/
public static void greet(String name){
System.out.println("Hello " + name);
}
/*
* Another static method which overloads above Hello method
* This shows that we can overload static method in Java
*/
public static void greet(String name, String greeting){
System.out.println(greeting + " " + name);
}
}
Output
Hello John
Good Morning John
* Java program to show that we can overload static method in Java.
*/
public class StaticOverloadingTest {
public static void main(String args[]) {
greet("John"); //will call a static method with one String argument
greet("John", "Good Morning"); //overloaded static method will be call
}
/*
* static method which will be overloaded
*/
public static void greet(String name){
System.out.println("Hello " + name);
}
/*
* Another static method which overloads above Hello method
* This shows that we can overload static method in Java
*/
public static void greet(String name, String greeting){
System.out.println(greeting + " " + name);
}
}
Output
Hello John
Good Morning John
That's all on How can we overload static methods in Java. In summary,
Don't confuse between method
overloading and method overriding. In short, you can overload static method
in Java but you can not override static method in Java.
Further Learning
SOLID Principles of Object-Oriented Design
Absolute Introduction to Object-Oriented Programming in Java
Java - Object-Oriented Programming [For Absolute Beginners]
Other Java OOPS tutorial from JDK67
Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course.
ofcourse you can not overload static method, but you can hide them in Java.
ReplyDeleteyou can overload but not override static methods
DeleteI am new to java. Your blog is really good. It helps me in learning java. Keep writing more articles.
ReplyDeleteIs it method hiding
ReplyDelete