• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final array!!

 
Tausif Khanooni
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • 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
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • 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?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hey.. Thank you both... really it is a great help!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic