File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overloading static methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overloading static methods?" Watch "overloading static methods?" New topic
Author

overloading static methods?

Phil Perkins
Ranch Hand

Joined: Nov 21, 2000
Posts: 40
Can you overload a static method?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
You can write a static method with the same signature as a parent class static method but it is not considered overloading.
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
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?


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Rick Salsa
Ranch Hand

Joined: Jul 17, 2001
Posts: 173
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
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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)
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
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.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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 ]

Rob
SCJP 1.4
Chris Graham
Greenhorn

Joined: Jan 08, 2002
Posts: 22
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

Joined: Dec 18, 2001
Posts: 417
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.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

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 ]

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overloading static methods?