*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static method overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static method overloading" Watch "Static method overloading" New topic
Author

Static method overloading

Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219


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?


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Yes, by giving the method a different parameter list you have overloaded the method and no longer have a duplicate method.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
So, a static method can be overloaded with a non-static method?
Kumar Kausikasa
Ranch Hand

Joined: Jun 04, 2002
Posts: 45
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
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
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 ]
Marcelo Sousa Ancelmo
Ranch Hand

Joined: Jan 17, 2002
Posts: 497

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.


Marcelo Sousa Ancelmo - Brazil
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Why not try compiling a program with this and see what happens?
Igor Romanov
Greenhorn

Joined: Nov 24, 2001
Posts: 23
...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 ]

cheers,<br />Igor Romanov
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
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.
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static method overloading
 
Similar Threads
Variable shadowing
Outer-Inner Question
compile error
[this]....
kindly explain the code.