File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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