This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
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