• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous classes defined in a method and final var

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The text books that I have read so far did not mention this restriction.
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What are you doing in my house? Get 'em tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic