• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static method overloading

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, by giving the method a different parameter list you have overloaded the method and no longer have a duplicate method.
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, a static method can be overloaded with a non-static method?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 498
Eclipse IDE Firefox Browser Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not try compiling a program with this and see what happens?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...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 ]
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic