This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Method Local inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Method Local inner classes" Watch "Doubt in Method Local inner classes" New topic
Author

Doubt in Method Local inner classes

Loganathan Karunakaran
Ranch Hand

Joined: Nov 04, 2008
Posts: 76
Please consider the below two classes:

I)



Output: Compilation fails

MyOuter2.java:8: local variable z is accessed from within inner class;
needs to be declared final
System.out.println("Local variable z is " + z);

II)



Output: Compiles fine

The only change in the second class is the variable 'z' is marked 'final'
In Kathy Sierra book, it is mentioned that the inner class object cannot use the local variables
of the method the inner class is in.


Even though we make it final, still its a local variable. Please help me to understand how it working when we make it final.


OCPJP 1.6
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://www.coderanch.com/t/524522/java-programmer-SCJP/certification/Special-treatment-final-vaiables-method
Loganathan Karunakaran
Ranch Hand

Joined: Nov 04, 2008
Posts: 76
Thank you Seetharaman

That was very fast repsonse.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Loganathan Karunakaran wrote:Thank you Seetharaman

You are welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt in Method Local inner classes
 
Similar Threads
Code for Method-Local Inner Classes not compiling
Method-local inner class and final local variable
where final variable reside ?
Doubts regarding Method local inner class
final loacl variables