aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from master exam 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 "question from master exam" Watch "question from master exam" New topic
Author

question from master exam

Alina Petra
Greenhorn

Joined: Oct 17, 2006
Posts: 26
Given:

What is the result?
A 4243
B 4342
C Compilation fails due only to an error on line #1
D Compilation fails due only to an error on line #2
E Compilation fails due to errors on both lines #1 and #2
F An exception is thrown at runtime

The answer provided is C. But i don't see any error on line #1.
Can anyone clarify this?



[HENRY: Added code tags]
[ October 28, 2006: Message edited by: Henry Wong ]
santro
Greenhorn

Joined: Sep 20, 2006
Posts: 5
We can't use super and this in static context
Alina Petra
Greenhorn

Joined: Oct 17, 2006
Posts: 26
Have you tried to compile the code above? There are no compilation errors.
Alina Petra
Greenhorn

Joined: Oct 17, 2006
Posts: 26
Sorry, i was wrong. The code does not compile because of the super within the static context. I focused on something else that's why the things got a bit mixed up

The answer is:
C is correct. Only instance methods can be overridden, and calls to super only apply to overridden methods.

In fact this answer got me confused. I haven't noticed that the call to super is done within a static context. That can not work, you are right.

But the explanation (
calls to super only apply to overridden methods
) is not correct, do you agree?
santro
Greenhorn

Joined: Sep 20, 2006
Posts: 5
Yaa i tried it with commenting the #1 line,it was successfully compiled
Burkhard Hassel
Ranch Hand

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

Alina Petra wrote
But the explanation (

calls to super only apply to overridden methods
is not correct, do you agree?
-


The language specification says, that static methods aren't overridden but hidden.
If you do call it also overriding, then the sentence would be not correct. Because you cannot call a static method with super.

Anyway, I think it's just easier to memorize if you say: "You can't use super or this in a static context."


Yours,
Bu.


all events occur in real time
Alina Petra
Greenhorn

Joined: Oct 17, 2006
Posts: 26
Because you cannot call a static method with super.


This is false. You CAN call static methods using super. Actually that was all about within my question.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

Originally posted by Alina Petra:

This is false. You CAN call static methods using super. Actually that was all about within my question.



Yes, you can call a static method using super. But in this case, the "this" (and "super") is not available -- since you are in a static context.

BTW, even if it is in context, it is not a good idea. You should access static methods via the class name (like AlternateFuel.getRating2()). It makes it easier to remember which methods are static and which are not.

Henry
[ October 28, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
I wrote
(...) you cannot call a static method with super.




As Alina already said, this is wrong.
I didn't knew that. But I also never tried until today.


Maybe, the note on page 104 (Chapter 2: Object Orientation) of the K&B book should be altered.
Says:
Note: Using super to invoke an overridden method only applies to instance methods. (Remember, static methods can't be overridden.)



Thanks, Alina

Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question from master exam