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

final modifier

Grady Jamyson
Ranch Hand

Joined: Aug 04, 2002
Posts: 42
I've done the following question,
but I don't understand why the answer is "d".
Could anyone explain it?
Thanks a lot.

class JSC201 {
static byte m1() {
final char c1 = '\u0001';
return c1; // 1
}
static byte m2(final char c2) {return c2;} // 2
public static void main(String[] args) {
char c3 = '\u0003';
System.out.print(""+m1()+m2(c3)); // 3
}}

What is the result of attempting to compile and run the program?

a. Prints: 13
b. Prints: 4
c. Compile-time error at 1
d. Compile-time error at 2
e. Run-time error
f. None of the above
Sachin Roham
Greenhorn

Joined: Aug 03, 2004
Posts: 19
Hey Grady,

If u check line 1...
return c1;
an implicit cast is put there 2 convert char to byte.
this is because c1 is a compile time constant...
(its value is known at complie time ie final char c1='\u001')

but in the case of line 2...
there is a problem cause c2 is not a compile time constant...
it is a definately a constant, but its value is not known at
compile time.. as it is an argument to a method its value can
change therefore the compiler complains at line 2.


Try my simulator
Grady Jamyson
Ranch Hand

Joined: Aug 04, 2002
Posts: 42
I got it. Thanks a lot.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Stoneds Brains please read the JavaRanch Naming Policy and change your displayed name accordingly.

Thanks
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
 
subject: final modifier