• 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

Declaring final array items

 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading about how you can "trick" a program into letting an anonymous inner class modify the value of a local variable. Since anonymous inner classes can only deal with variables declared final, the suggestion is creating an array, declaring the array final, and then using the anonymous class to modify the values within the array.
Since using final on an array means that you can't assign any other array to that variable, but can still modify the elements within it, I was wondering if there is a way to declare the elements within the array as final? Would you ever really want to do this in the first place?
I tried
but obviously it is just sticking a copy of the value.
Thanks
Jason

[This message has been edited by jason adam (edited November 09, 2001).]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't do it with an array, but you can use a List and wrap it inside a Collections.unmodifiableList(). The trick you mentioned is not something I'd use in Real Life[tm], by the way.
- Peter
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya, I don't think I've ever come across anything that would require it, it was just mentioned as a work around, which got me curious about making an array of final elements.
Thanks for the info
Jason
 
reply
    Bookmark Topic Watch Topic
  • New Topic