aspose file tools*
The moose likes Beginning Java and the fly likes Overriding Static Methods, Huh? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overriding Static Methods, Huh?" Watch "Overriding Static Methods, Huh?" New topic
Author

Overriding Static Methods, Huh?

Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
Ok, I'm having some trouble grasping a concept here for overrindg a static method. I just dont see why you would do this and even if you did, how it's supposed to function. I'm currently reading the SCJP5 Study (Sierra & Bates) guide and I just cant seem to understand the whole overriding thing.

What I get from the book is simply:

1. You can do it
2. If you do it doesnt do anything.

The book has the following code, which I follow completey however I just do understand *WHY* you would ever want to do this.

Can somone shed a little more light on the why and when you would want to do this. The book unfortuneately doesnt cover this in depth all that much.



Output is:

a a a

Is the whole thing here the dostuff() (Note no capital S) in the method name? I missed that the first time through but it would seem to be a typo since the it doesnt mention that the name is different. If that is not a typo why even mention it? It's just a different method name. If it is a typo then my first 2 questions apply. Why? When?

Thanks in advance for any replies. I just want to be sure i'm understanding the override concept completely.


Rob Mech, SCJP 1.5<br /><a href="http://www.robsprogrammingjunk.com/" target="_blank" rel="nofollow">http://www.robsprogrammingjunk.com/</a>
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

Rob,
Welcome to JavaRanch!

Let's assume it is a typo since (as you noticed) the example doesn't show much without that assumption. The example is showing that Animal.doStuff() still calls Animal's version of the static method. While you are allowed to create a subclass method with the same signature, it won't get called unless you explicitly write Dog.doStuff().

As for why one would want to this: If someone wanted to write confusing code, this approach could be useful . Seriously, I would avoid doing this in practice as it makes code that is hard to understand and can trip people up who aren't familiar with all the nuances of the language.

I think the book doesn't cover the why much because it isn't something you would want to do. But if the anti-pattern is on the exam, they have to cover it.

To summarize, you would want to override and instance method, not a static one.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Actually this is not an example of overridding.

Static methods cannot be overridden. If there is a static method in a subclass that has the same signature as one in the class it extends, then that is called hiding or redefining.

This differs a great deal from the concept of overridding.

When a method is overridden, then the invocation to be called is decided at runtime based on the runtime type of the caller.

When a method is hidden, then the invocation is decided based on the type of the reference.

Consider this example.



The output is

Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
Sorry, I used the word "Override" when I mean "Redefined" as the book says. But if I'm to interpret the last example correctly, does this redefinition take place during Instatiation only and differs from the statc method call itself?

Also, in the previous response you mention Dog.doStuff(); however in the code call, it does reference a Dog instance of the class.

I think i'm confused more now than before. Can we summarize this?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

static methods belong to the class.

They are NOT polymorphic like non-static methods.

If you have a ADS(*) as above,
only the reference type counts. Runtime type does not matter:

Animal poly = new Dog();
poly.doStuff(); // static method

will call the static method of Animal, not of Dog when redefined as in the example.
A really overridden non-static method would call the Dog-method.



You could call the dog method through an instance of type Dog, not animal, so
Dog doggy = new Dog();
doggy.doStuff();

would call the Dog method.

And remember: This way of calling a static method through an instance is allowed, but bad and confusing. My Eclipse environment sets up a warning if I do so, BTW.

Therefor always say:
Animal.doStuff();
Dog.doStuff();



Yours,
Bu.

(*) ADS: animal dog situation
Welcome to the Ranch, Rob!
[ April 14, 2007: Message edited by: Burkhard Hassel ]

all events occur in real time
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See Overriding vs. Hiding.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
And remember: This way of calling a static method through an instance is allowed, but bad and confusing. My Eclipse environment sets up a warning if I do so, BTW.


Youre right on the confusing part. Im only asking this question so when it shows on the test I can answer it.

Ok, let me summarize then to make sure I understand.

If a Static method is declared and redefined as in the first example in this thread. Any reference calls to it Dog.doStuff() or Animal.doStuff() call the doStuff() in the Animal class.

If I instance the object though, it calls the appropriate one so long as I reference the static method with the appropriate reference?

If this is correct then is the example above (read the first one from the book) incorrect? Or is the trick here referencing the class NAME itself i.e. Dog.doStuff vs. a.doStuff() ?

Does everyone see where I'm confused here? It would appear as if the book example is incorrect?

I'm sorry if this seems like I'm not catching on, but on this particular nuaince I'm not!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rob Mech:
...let me summarize then to make sure I understand.

If a Static method is declared and redefined as in the first example in this thread. Any reference calls to it Dog.doStuff() or Animal.doStuff() call the doStuff() in the Animal class.

If I instance the object though, it calls the appropriate one so long as I reference the static method with the appropriate reference? ...

A good way to learn is to write test code to investigate and/or verify your understanding. For example...

(Note: Java is case sensitive. So doStuff is not the same as dostuff. Without correcting this in your code, you will get very misleading output.)

So what do you conclude from the output of this program?
[ April 14, 2007: Message edited by: marc weber ]
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
Originally posted by Rob Mech:

If a Static method is declared and redefined as in the first example in this thread. Any reference calls to it Dog.doStuff() or Animal.doStuff() call the doStuff() in the Animal class.


Nope: Dog.doStuff always calls the Dog-method and Animal.doStuff always calls the Animal-method. objectreference.doStuff will call the Animal-method if objectreference is a variable of type Animal (even if the actual object it references to is really a dog). So, in your example a[x].doStuff() will result in the same behavior as Animal.doStuff() would, just because a is an Array of Animals.
[ April 14, 2007: Message edited by: Sasha Ruehmkorf ]
Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
UGH!!!

I can't BELIEVE that I missed this. My Whole point of confusion here was why the sample code output an a instead of a b.

What I failed to notice (and would have smoked me on any test) was the fact that the array was declared as follows.



It's using the superclass for the reference. How could I have missed that? Now this ALL makes sense with regards to WHY the super method is called for the code. I finally got it

Now, my next big question here. How is this any different then overriding? Why the term "redefined"? Unless I'm still missing something they semantically do the same thing.

Thanks everyone for the patience. I do appreciate it.
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
Originally posted by Rob Mech:
Now, my next big question here. How is this any different then overriding? Why the term "redefined"?

Keith cleared it all:
Originally posted by Keith Lynn:
When a method is overridden, then the invocation to be called is decided at runtime based on the runtime type of the caller.
When a method is hidden, then the invocation is decided based on the type of the reference.


If your doStuff methods were not static (and without the typo) your code would produce the output a d a.
In this case you have an overridden method.
Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
Thanks All, I got it.
 
wood burning stoves
 
subject: Overriding Static Methods, Huh?