This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods & Overriding!" Watch "Static methods & Overriding!" New topic
Author

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:
Base.hey();
Derived.hey();

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
Greenhorn

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 only.so 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 classes...it 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...

uufff....
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: http://aspose.com/file-tools
 
subject: Static methods & Overriding!
 
Similar Threads
Static method
Stack Overflow
why static methods cannot be overriden
Upcasting
Static methods and instance variables are not overridden