File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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.
Valentin Crettaz
Gold Digger

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

From JLS 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.

[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().....
another one
I got about 25 results.
good luck
[ 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:
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,
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="" 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:
subject: static methods
It's not a secret anymore!