File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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!!
It's not a secret anymore!