• 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

final array!!

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If I define an final array, I can not use this
statement:
eg. final int i[] = {1,2,3};

i = someOtherArray; // will not compile.
but in case of changing the value of array like:
i[0] = -99; //it is okay.
In case I want to keep the array values also as
final, In what way I can acheave this?
Hope I made my point clear.
Thanks
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't make an object final using the "final" keyword, you are only making the reference variable final (as you noticed). I am not sure if there are any fitting class wich achieves this.
Why do you need this behaviour by the way?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way to achieve what you want is to wrap the int primitive within Integer objects, stuff those objects within an ArrayList and obtain an unmodifiable list from the Collections class, as depicted in the following code:

There is unfortunately no way you can prevent someone from changing the value of an array component
 
Tausif Khanooni
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey.. Thank you both... really it is a great help!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic