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 & Overriding! 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 & Overriding!" Watch "Static methods & Overriding!" New topic

Static methods & Overriding!

Nadeem Khan
Ranch Hand

Joined: Nov 27, 2007
Posts: 108
We say that static methods arent overridden i.e they dont follow the same principles of overriding. For Example-

So my question is then why do we HAVE to declare our static method in derived class (with same name & arguments as parent's method) with same or stronger access modifier??
That is why cant i assign hey() of derived class a private access modifier when its not being actually overridden? I dont see any harm in it as it can't be called by derived's super classes!!

<i>If there were no Exceptions (not Errors!), Life would have kinda sucked!!</i>
Daniel Chemko
Ranch Hand

Joined: Feb 27, 2008
Posts: 32
You should be using static methods like they're designed to be used for, by not calling them from instance objects.

Proper access to each of these methods would be:

Here's an example to demonstrate static methods and static code blocks too.

Nadeem Khan
Ranch Hand

Joined: Nov 27, 2007
Posts: 108
Hi Daniel,
Got your point about correct static usage. But my question was
that why cant i declare two static methods (with same name & args)
- in the derived class with a private access modifier
- and as public in base class
( as its not getting overridden) ?
Aarti Gauba

Joined: Feb 29, 2008
Posts: 2
We say that it is not getting overridden because the version of method being called is decided at compile time and not runtime. whereas in overriding, the version of method to be called is decided at runtime depending on the object type.

but for static methods, dis is decided at compile time it is not overridden. but it does not mean it is not following the rules of overriding. it follows the rules of access modifiers and exceptions n all...when 2 methods with d same name ,same arguments ,etc..etc.., are there in 2 is called overiding...and it is d same for static methods... but dey are not being overriden.... soo the diff between calling it overriding..and actually being overriden...

Nadeem Khan
Ranch Hand

Joined: Nov 27, 2007
Posts: 108
thats my point..since its not getting overridden why to follow those rules? Suppose static methods dont follow those rules, its not going to create any exception or confusion. The rule is simple ..If you are a class (or an instance) and you have a static method, you can only call your own method (with that specific name & args!) OR you can call the static methods you have inherited (but not declared your own methods with same name & args)!!!
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
It is not because of any rules, it is a consequence of how computers work. There is only one, and can be only one of anything that is static: Whether function or not. It gets laid out when the compiler/jvm working together make a whatever it is the programmer has written.

If you need more than one of them, you call new. If the coder makes it static, then it goes when the compiler loads the class and is a consequence of the way machinery operates ~ not a consequence of any rules.

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
I agree. Here's the link:
subject: Static methods & Overriding!
It's not a secret anymore!