• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Method Local inner classes

 
Loganathan Karunakaran
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Loganathan Karunakaran
Ranch Hand
Posts: 78
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Seetharaman

That was very fast repsonse.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loganathan Karunakaran wrote:Thank you Seetharaman

You are welcome
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic