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 can't be overidden why 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 can Watch "static methods can New topic

static methods can't be overidden why

nagaraj raja
Ranch Hand

Joined: Aug 06, 2005
Posts: 36
I am thinking static methods are loaded at the time of compilation so static methods can't overidden at runtime is it right?plz clarify my dout....
Ola Daniel
Ranch Hand

Joined: Jul 27, 2005
Posts: 105
I overload my static methods all the time, so they can be overridden...

SCJP 1.4, SCWCD 1.4
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler et al, Refactoring: Improving the Design of Existing Code, 1999
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Static methods can't be overriden. Just try it. You will fail.

SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Originally posted by David Ulicny:
Static methods can't be overriden. Just try it. You will fail.

I tried it here:
I succeeded !!

<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Struan Kerr

Joined: Feb 01, 2005
Posts: 10
static methods are linked at compile time (like variables) so if you override them they get hidden.

so if you have two classes:

and then in the following code you do something like:

the output will be:

because as far as the compiler is concerned champion is an animal not a horse.
[ August 16, 2005: Message edited by: Struan Kerr ]
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
This is the output:

So, you just call constructor Test() which calls constructor Base() and inside both constructors you just call both methods staticMethod(). I cannot see overriden static method.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Static methods are *not* overridden. Semantic nitpickiness follows:

�, JSL
If a class declares a static method m, then the declaration m is said to hide any method m', where the signature of m is a subsignature (�8.4.2) of the signature of m', in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.
Swapna Anand

Joined: May 11, 2005
Posts: 7
Static methods can only be overridden by Staic methods, they cannot be overridden by instance methods in the child class.
hope this clears up the doubt!!!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"Swapna", please read our JavaRanch Naming Policy, and change your displayed name to conform. We require two names in the format <first name><space> <family name>, preferably your real name.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Dharmesh Gangani
Ranch Hand

Joined: Feb 20, 2004
Posts: 30
I am still not able to understand it completely. can anyone explain that in a little more detail ?

Thanks in advance.
- Dharmesh

-=-=-=-=-=-=-=-=-=-<br />Thanks & Regards,<br />Dharmesh G.
sameer inamdar
Ranch Hand

Joined: Jul 04, 2005
Posts: 40
Now I got confused I need to review the concept again.

Dream!!! Set Vision !! Transform Vision to Mission by Actions on Deadlines!.
Arul Prasad
Ranch Hand

Joined: Jan 20, 2005
Posts: 57
static methods are linked a compile time (like variables) so if you override them the get hidden.

if u take this code u can call the static method with out create a instance static members are direclty related to the class

the output was
staticMethod() in Subclass Test.
staticMethod() in Class Base.

So when u call the static method using the Child Class i.e DemoTest.staticMethod() it will hide the base class implementation and it executes the child class implementation.

again if u call the method using the DemoBase.staticMethod() it will execute the base class implementation ...

so instance does not hava a role in this case ... So static methods can be override but it was linked to the compile time(static) so it will try to hide the implementation....

supoose take this scenario

the output was ..

staticMethod() in Class Base.
staticMethod() in Subclass Test.
staticMethod() in Class Base.

see when i was calling DemoTest1.staticMethod() it give me the output of base class since DemoTest1 child class of DemoBase was not overridding the staticMethod() so the implemention of the base class was not hidden..

With Regards<br />Arul
Naveen Punati

Joined: Apr 13, 2005
Posts: 28
Hi all,

Create a child class object and assign it to Parent reference.

Now call any static method that has same signature in both the classes(parent & child), ur parent static method will be called since ur reference is of parent type.....

Even though ur object is of child type.... the binding happened during compile time and during compile time the compiler knows only about the type of reference(parent) not the object(child) to which it is refering to.That is why ur parent static method will be called.
I agree. Here's the link:
subject: static methods can't be overidden why
It's not a secret anymore!