• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

overloading static methods?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you overload a static method?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can write a static method with the same signature as a parent class static method but it is not considered overloading.
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
William, when you say signature, what do you mean by that? Does it mean that it has the same name takes the same number of parameters and has the same return type? Or a combination/omission of these?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JLS- If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded.
There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures.
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason,
>>Does it mean that it has the same name takes the same number of parameters and has the same return type?
Yes, except for the return type. The signature is the method name and the list of parameters. Return type is not part of a method's signature.
/rick
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Phil Perkins:
Can you overload a static method?


public class A {
static void mm()
{System.out.println("mm");}
static void mm(int a)
{System.out.println("mm");}
}
method mm() is static and we overloaded it.
where is the problem. i am not sure if this is the question you were asking. methods can be overloaded. period. ( static or non-static )
or are you asking overriding, then also it is fine
class B extends A {
static void mm()
{System.out.println("mm-overriding");}
}
method mm() is overridden in class B and is ok. so again methods can be overridden (static or non-static)
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mark stone:

public class A {
static void mm()
{System.out.println("mm");}
static void mm(int a)
{System.out.println("mm");}
}
method mm() is static and we overloaded it.
where is the problem. i am not sure if this is the question you were asking. methods can be overloaded. period. ( static or non-static )
or are you asking overriding, then also it is fine
class B extends A {
static void mm()
{System.out.println("mm-overriding");}
}
method mm() is overridden in class B and is ok. so again methods can be overridden (static or non-static)


Static methods don't participate in overriding, in the example you have shown the method mm() is not being overridden, if you read the JLS part that Ajith has pointed out, you will understand it.
Vanitha.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark.
You're right about being able to *overload* static methods; there is no problem there. But you cannot override static methods; they are *not* inherited by an object, they reside with a class. If you redefine a static method in a subclass with the same signature as in it's super-class, you have not overriden it, you have "Hidden" it. See JLS 8.4.6.2
Here's an example program I hope makes this clear:

Hope this helps!
Rob Ross

[ January 08, 2002: Message edited by: Rob Ross ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While this article doesn't address overloading static methods directly, it's a great dialog on Static and Instance methods, with some really cute examples. It will give you some ideas on when using static methods is appropriate.
Hope everyone finds it useful!
http://www.javaworld.com/javaworld/javaqa/2001-11/03-qa-1121-mrhappy.html
--Chris
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes the terminology for static methods is not overridden. Don't get me wrong here but i was just trying to put the point across. But you would agree that it is just mere words that we define hide or shadowing instead of overriding.
right ?

Originally posted by Vanitha Sugumaran:

Static methods don't participate in overriding, in the example you have shown the method mm() is not being overridden, if you read the JLS part that Ajith has pointed out, you will understand it.
Vanitha.

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

But you would agree that it is just mere words that we define hide or shadowing instead of

I for one don't want to accept that these are just mere words. Sorry to disagree.
You hide or shadow by making them private or protected or package level access.
But you are....
overriding
to change the behavior of something.
Having said that I would argure in favor of the fact that the word overriding used on static or private methods doesn't make sense and so would not argue about that. Neither argument wins.
For what its worth.....
- satya
[ January 08, 2002: Message edited by: Madhav Lakkapragada ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic