The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static method 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 method overriding" Watch "static method overriding" New topic

static method overriding

Ramesh Kale

Joined: Jun 21, 2004
Posts: 13
This is the example i got from dans exam

class E {
void printS1(){System.out.print("E.printS1 ");}
static void printS2() {System.out.print("E.printS2 ");}
class F extends E {
void printS1(){System.out.print("F.printS1 ");}
static void printS2() {System.out.print("F.printS2 ");}
public static void main (String args[]) {
E x = new F(); x.printS1(); x.printS2();

The result is
F.printS1 E.printS2
Any one have any idea about the static method overriding. I think static method always call on reference.
Please correct me... if I am making mistake.

Regards<br />Ramesh<br />(SCJP 83%)<br />Failure is part of success!!!
Doyle Matt
Ranch Hand

Joined: Jul 05, 2004
Posts: 76
Yes you're right. static method calls are determined by the type of reference they have and not the object that the reference contains.

SCJP 1.4
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
static methods are never overloaded, what your doing is called hiding. You can do the same with variables as well(rules are different). So basically you can use the refereance type to access the hidden method.
however if you did this F x = new F(); and did x.printS2() the output would be F.printS2.

Here is more about hiding

variable hiding
method hiding

MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please read Corey's Tip about this.

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

Joined: Jul 05, 2004
Posts: 76
static methods are never overloaded
maybe you mean "static methods are never overridden"
I agree. Here's the link:
subject: static method overriding
It's not a secret anymore!