Forums Register Login

Static method overloading

+Pie Number of slices to send: Send


1) Changing the code at line 10 to the following, will allow the code to compile correctly:
public void print(int x){
Is this statement correct?
+Pie Number of slices to send: Send
Yes, by giving the method a different parameter list you have overloaded the method and no longer have a duplicate method.
+Pie Number of slices to send: Send
So, a static method can be overloaded with a non-static method?
+Pie Number of slices to send: Send
Hi
By declaring a method static , the method would execute without constructing a instance of the corresponding class.
So when your main starts executing, even the static test would be executed, irrespective you create an instnace
Test t = new Test();
But for the non static methods , you have to create an instance to invoke methods in the class.
As far as overloading goes, the methods must have different signatures, means return type and number of arguments. so "static" does not come into picture if this was a overloading question.
Praveen
+Pie Number of slices to send: Send
Thanks praveen, but I read somewhere that static methods can be overloaded only by static methods. I know, If a method differs with arguments AND/OR return types, it is overloading.
My question is, is this overloading? If yes, is it possible to overload staic method with instance method?
Thanks
[ June 14, 2002: Message edited by: Thiru Thangavelu ]
+Pie Number of slices to send: Send
I think you read "Static methods cannot be overriden by non static methods", overload allow you to have methods with the same name, as long the parameters differ in type and number.
+Pie Number of slices to send: Send
Why not try compiling a program with this and see what happens?
+Pie Number of slices to send: Send
 

...I know, If a method differs with arguments AND/OR return types, it is overloading...


Return type is not a part of method's signature.
Overloading method can have different return type.
Only arguments types and order are important.

...If yes, is it possible to overload staic method with instance method?...


The point here is to understand a big difference between overloading and overriding methods. Overloaded methods are completely independent from the compiler's point of view. The same name is just for programmer's fun. But compiler determine which method to call not by name only, but by method's signature. This is method's name and argument types (order, count).
Argument names are not important - there's no more names when you make a method call. Return type is not important due to possible implicit type changes.
That is - if you have two mathods declarations:
int foo( int a, int b );
float foo( int a, int ? );
And late smth. like this:
double j = foo( 10, 15 );
No way to decide which one is to call 8(
So, yes, you can have to completely separated (from compiler's point of view) methods, and one of them is static while another one is not.
[ June 15, 2002: Message edited by: Igor Romanov ]
+Pie Number of slices to send: Send
Hi Thiru Thangavelu,
Your question:
"My question is, is this overloading? If yes, is it possible to overload staic method with instance method?"
The answer is very simple. If two methods have
two different signitures, why do we have worry
about which one is static and which one is not?
They have nothing to do with each other except
that they have the same method name.
+Pie Number of slices to send: Send
It is merely an overloaded method which doesn't care any modifier including static as long as the argument list is different and method name is same.
Thanks for all of you, I understood the concept.
No holds barred. And no bars holed. Except this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1101 times.
Similar Threads
Variable shadowing
compile error
kindly explain the code.
[this]....
Outer-Inner Question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:24:37.