Author Topic:   Can u overload static methods with non static ones??
Nalini Mistry
ranch hand
posted May 06, 2000 07:29 AM             
We know that static methods cannot be overridden with non static methods and vice versa. but what about overloading?? can i have a static method with one sign, and a nonstatic method with the same name, diff sign??

ranch hand
posted May 06, 2000 11:56 AM             


The answer to your is yes and no. Let me explain.

In a class we know static methods (variables) belong
to the class and not to any particular instance. Keep
this in mind.

Now coming to the question:


public static aMethod() {
System.out.println("From static aMethod()");

public void aMethod(int i) {
System.out.println("From aMethod()");

public static void main(String[] arg){

StaticOverload sOver = new StaticOverload();
// sOver.aMethod(50); Lucky this is commented

Save the code in file StaticOverload and the remaining

The above code will compile and run. With this code my answer
is YES. A static method CAN be overloaded.

Coming to the NO part of my answer, I don't mean to contradict
what I said earlier, but all I want to say is its USELESS.
When you uncomment the line of code which calls the overloaded
method (the one with int), it does not compile. The error is
"Worng number of arguments in method call". The overloaded
method is never seen.

Also, it doesnot help even if I declare the overloading
method (the one which takes the int argument) to be static.

So, my final answer is:

Can you overload a static method ?
YES, I can overload a static method.

Can you invoke an overloaded static method?
NO, I cannot invoke the overloading method. I will get an
error saying "worng number of arguments in method call."

Hope this helps.

- satya

ranch hand
posted May 06, 2000 02:02 PM             

I tried your program by uncommenting the line you commented out and it's working fine. I don't understand why you can't call an overloaded static method. When you overload a method it will be totally a different method. IMO you can overload a static method to be non static or static and you can even call that overloaded method it works fine as long as the parameter list is different.


ranch hand
posted May 06, 2000 02:20 PM             


You are absolutely right. Sorry I was wrong in my earlier post.
(I had two method names aMethod and aMethod1(int i) which is not

Sorry, Sorry !!!!!!!

So, in essence, Static methods can be overloaded and invoked
without any problems. All rules that apply for overloaded
methods apply likewise.

Thanks for checking on me Sree.


- satya

Nalini Mistry
ranch hand
posted May 07, 2000 04:38 AM             
thanx a lot guys!!

maha anna
posted May 07, 2000 01:44 PM             
To finish up I am adding this.

A overloaded method is completely new born method which just happened to have the same name of the other method. It is asthough you are defining a NEW METHOD. So this method can have any parameter/parameter type/parameters order/access level/throw any exception/can have any other sutable method modifiers. You can overload in the same class OR in the subclass also. In both cases ,still the new overloading method is a NEW BORN method and all the info said above still applicable.

maha anna

Nalini Mistry
ranch hand
posted May 07, 2000 11:35 PM             
Thanx MA. I think something similar has come up before with private methods and overriding. I get it now.