jQuery in Action, 2nd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final Variables -- No . 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Final Variables -- No . 2" Watch "Final Variables -- No . 2" New topic
Author

Final Variables -- No . 2

padmini Babu
Ranch Hand

Joined: Feb 10, 2001
Posts: 103
What is the result of attempting to compile the following code and then execute the class Outward?
1. class Outward {
2. final int i = 10;//LINE 1
3.
4. public static void main(String[] args) {
5. new Outward ();
6. }
7.
8. Outward {
9. (new Inward()).foo();
10. }
11.
12. class Inward {
13. void foo() {
14. int i = 20;
15. System.out.println("i = " + i);
16. }
17. }
18. }
D) Line 15 prints out "i = 20".
Correct selection is: D
The code has two variables with the name "i". The one with the innermost scope (on line 14) shadows the one on line 2, so line 15 prints out "i = 20".
DOUBT: WHY DOES SHADOWING OCCUR HERE AND WHY DOES IT NOT OCCUR IF I REMOVE FINAL FROM LINE 1.
IT IS SAID THAT FINAL VALUES CANNOT BE ASSSINGED ONCE AGAIN BUT HERE IT IS REDECLARED.
Thanks in advance
Padmini
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356

Hi,
In the method foo() int i is local variable. So changes made on this local variable doesn't affect the final variable.
Vanitha.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Final Variables -- No . 2
 
Similar Threads
book K&B self test q16........problem
inner class visibility
Casting