This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: can we overload static methods in java example
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Method overriding is not possible but method overloading is possible for static methods.
Before that lets see about method overloading in java.
Method overloading:
Defining multiple methods with same name and with different arguments is known as method overloading.
Multiple methods with same name and different arguments so compile time itself we can tell which method is going to get executed based on method call.
Method overloading also known as compile time polymorphism.
Static methods - method overloading
Its always possibles static method overloading.
Defining multiple static methods with same name and different arguments will possible.
By this we can define multiple main methods in our class with different arguments but only default main method will be called by the JVM remaining methods we need to call explicitly.
Lets see an example java program which explains static method overloading.
class StaticMethodOverloading{
public static void staticMethod(){
System.out.println("staticMethod(): Zero arguments");
}
public static void staticMethod(int a){
System.out.println("staticMethod(int a): one argument");
}
public static void staticMethod(String str, int x){
System.out.println("staticMethod(String str, int x): two arguments");