Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods

 
Hung Tang
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob E. Lee
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic