This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final array!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final array!!" Watch "final array!!" New topic

final array!!

Tausif Khanooni
Ranch Hand

Joined: Nov 14, 2002
Posts: 107
If I define an final array, I can not use this
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.

"Walking on water and building IT Architecture from <br />specification are easy if and only if both are frozen"
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
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

Joined: Aug 26, 2001
Posts: 7610
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

[Blog] [Blogroll] [My Reviews] My Linked In
Tausif Khanooni
Ranch Hand

Joined: Nov 14, 2002
Posts: 107
Hey.. Thank you both... really it is a great help!!
I agree. Here's the link:
subject: final array!!
jQuery in Action, 3rd edition