*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "static methods" Watch "static methods" New topic
Author

static methods

Hung Tang
Ranch Hand

Joined: Feb 14, 2002
Posts: 148
Hi all, I just have a question about static methods. May a subclass override a static method from its superclass and declare it as non-static method? or vice-versa? I've tried it and it would not allow me to do it. I am having some doubts because I thought I've done a mock exam question that had asked me this and the answer (I thought) was that it was indeed possible. Please correct me if I am wrong.
Thanks.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
me63,
Welcome to Javaranch
No you are not allowed to do that.
From JLS 8.4.6.1 Overriding (by Instance Methods)
A compile-time error occurs if an instance method overrides a static method.

From JLS 8.4.6.2 Hiding (by Class Methods)
A compile-time error occurs if a static method hides an instance method.

Moreover, I'd like you to modify your publicly displayed name to comply with the Javaranch Naming Policy. Thank you.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Bob E. Lee
Ranch Hand

Joined: Jan 15, 2002
Posts: 36
Hey Hung
if you do a search on "static overridden" you'll found lots of threads on this topic.

Static method().....
and
another one
I got about 25 results.
good luck
Bob
[ February 16, 2002: Message edited by: Bob Tai ]
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Static methods belong to the class, and thus don't need an instance object to allow them to be called. When you call a static method you need to specify the following syntax indicating the class:
class.staticMethod()
Overridding a method has to do with polymorphic functions, which is a nice way of saying dynamic (runtime) binding that occurs based on the referenced type.
Back to static methods... Since you have to call a static method knowing ahead of time the class involved,
class_name.method_name()
what you're actually doing at the coding level is called static binding which takes place at compile time and not runtime. So you can't have polymorphism, which means you can't override using static binding
Just another reason it doesn't make too much sense to be able to override static class methods.


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods
 
Similar Threads
Method is static, yet I get compiler error saying it cannot be accessed from static context
can static methods be overridden?
why local classes in a static method?
Overloading & Overriding
RenentrantReadWriteLock on static method