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 Dan's Q 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 "Dan Watch "Dan New topic

Dan's Q

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Question 19
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
What is the result of attempting to compile and run the program?
a. Prints: P.printS1 P.printS2
b. Prints: P.printS1 Q.printS2
c. Prints: Q.printS1 P.printS2
d. Prints: Q.printS1 Q.printS2
e. Run-time error
f. Compile-time error
g. None of the above
---The answer is b.
I thought the answer would be d. Since static methods are not overridden, I thought Q's static method would be called. Can someone explain?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
This is an excellent and very common question here in this forum.
have a look at this post.
this post might be helpful as well.
[ October 15, 2003: Message edited by: Andres Gonzalez ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Thanks Andres. This question was awesome
Louis - Jean Morisset

Joined: Aug 25, 2003
Posts: 20
I would like to have more information when you say that static methods are not overriden?? I thought that you could override a static method no problem. You seem to even do so in the snipet of code that is there?? Maybe I don't understand what you mean.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The static method P.printS1 has been hidden by the subclass method Q.printS1(). P.printS1 has not been overridden by Q.printS1(). The following is the explanation that appears in the exam.

Suppose a method m1 is invoked using the method invocation expression m1(). If m1 is a static member of the class where the invocation expression occurs, then that is the implementation of the method that is invoked at run-time regardless of the run-time type of the object. If m1 is non-static, then the selected implementation is determined at run-time based on the run-time type of the object. The program invokes method printS1S2 on an instance of class Q. The body of method printS1S2 contains two method invocation expressions, printS1() and printS2(). Since method printS1 is static, the implementation declared in class P is invoked. Since printS2 is non-static and the run-time type of the object is Q, the invoked method is the one declared in class Q.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
I agree. Here's the link:
subject: Dan's Q
It's not a secret anymore!