Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final modifier

 
Grady Jamyson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Grady Jamyson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thanks a lot.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stoneds Brains please read the JavaRanch Naming Policy and change your displayed name accordingly.

Thanks
-Barry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic