aspose file tools*
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
Author

static methods

vamsi kantamneni
Greenhorn

Joined: Mar 14, 2013
Posts: 6


in the above example given in K&B
It is clearly said that the static methods cannot be overridden. I have a few doubts regarding this
1. A method is said to be overridden if the signature of the method is same in both the base class as well as the inherited class. In the above given example, I know that the methods are static and they load in the stack. how that is different from overriding??
2. if i remove the static keyword in the inherited class (Dog in this case) what would be the result?? (I have tried in the ecliplse.. but couldnt make anything out of it!!)
3. What if i dont want to use the static keyword in the inherited class (dog->doStuff()) (I have tried this too.. it said you cannot override the static method.. I m not overriding or anything.. jus trying to write another non-static method with the same name!!)

Please help me out of this confussion.....
Guy Buyle
Greenhorn

Joined: Nov 30, 2012
Posts: 6
vamsi kantamneni wrote:

in the above example given in K&B
It is clearly said that the static methods cannot be overridden. I have a few doubts regarding this
1. A method is said to be overridden if the signature of the method is same in both the base class as well as the inherited class. In the above given example, I know that the methods are static and they load in the stack. how that is different from overriding??
2. if i remove the static keyword in the inherited class (Dog in this case) what would be the result?? (I have tried in the ecliplse.. but couldnt make anything out of it!!)
3. What if i dont want to use the static keyword in the inherited class (dog->doStuff()) (I have tried this too.. it said you cannot override the static method.. I m not overriding or anything.. jus trying to write another non-static method with the same name!!)

Please help me out of this confussion.....


You can't override an static method but problem above is a kind of method hiding
every thing is clarified when you follows these link: http://www.coderanch.com/how-to/java/OverridingVsHiding

Regards,
Guy
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 812
    
    1
Static methods belong to their classes.
Polymorphism does not apply to static methods.
Animal.doStuff() will behave in "Animal" way. Dog.doStuff() will behave in "Dog" way.
How about this?


Why? myPet is an Animal and it behave in "Animal" way. Polymorphism won't apply to static methods in this case.
Ameen Imtiaz
Greenhorn

Joined: Mar 18, 2013
Posts: 11

First of all, overriding has to do with Polymorphism. The overriding methods are selected AT runtime based on the instance's type in the ref. variable at that moment.

Whereas, the static methods invocation is always decided at compile time. So if an instance variable invokes the static method then the compiler actual replaces the obj with the class (type) of that ref. variable. All this is explained on page 150 of K&B (Ch 2). Which states:

"
int frogs = f.frogCount; // Access static variable FrogCount using f

...This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a staticmethod or variable...
... In other words, the compiler cares only that reference variable f is declared as type Frog...
"


OCPJP (6)
 
 
subject: static methods