Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous classes defined in a method and final var

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The text books that I have read so far did not mention this restriction.
 
Dhanashree Mankar
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it is write. Anonymous class can access only final variables from enclosing method.
Here anonymous class extends thread class which is implementing run() method.
 
Vicky Jain
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barkat Mardhani,
In the code you are creating the method-local inner class which is the subclass of Thread. Also, method-local inner class can't access the local variables unless they are made final. This is perfectly mentioned in the chapter 8 of K&B book.
 
chang Anne
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
method-local inner classes can't access local variables inside Outer Class's method, ulness local variables are marked final.
I know a variable is marked final means can't change the value.
But why, method-local inner classes can access local variables are marked final?
Thank you.
 
chang Anne
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
method-local inner classes can't access local variables inside Outer Class's method, ulness local variables are marked final.
I know a variable is marked final means can't change the value.
But why, method-local inner classes can access local variables are marked final?
Thank you.
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&B give a good first half of an explanation of why the local variables must be declared final. They point out that the local/anonymous class may have a life beyond the existence of the variables on the stack. They don't say how java solves this problem. My understanding is that these classes DON'T operate on these variables. Instead, they create and hold COPIES of them. In order to guarantee that the variables and the copies don't get out of sync, java insists the variables be final.
Do I have that right?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,
I guess my understanding same as you stated.
The reason I started this thread was that in K&B book they talk about (non-anonymous) method-local class.
I came accross with a mock exam (Val's a very good and tough one) with a question where anonymous method-local class was trying to access method variable. I thought it is worth bringing up here because book does not mention that.
Thanks
Barkat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic