i want to have final array i.e the elements in that array should be final is it possible
Steven Bell
Ranch Hand
Joined: Dec 29, 2004
Posts: 1071
posted
0
No, if you declare an Array final you cannot change what that reference points to, but you can change the Object which means adding, removing Objects from the Array.
the final keyword does not indicate that the referenced Object cannot be changed, it means that the reference cannot be pointed to another Object.
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
You cannot add or remove objects from an array. You can reassign an element of the array, which may be a reference type.
[ March 01, 2005: Message edited by: Aruna Agrawal ] [ March 01, 2005: Message edited by: Aruna Agrawal ]
ashok kumar
Ranch Hand
Joined: Feb 28, 2005
Posts: 30
posted
0
i think all mistaken my question i know that
final int[] a={1,2,3};
in this we can change the elemnts value and we can't change the object refernce.
i am asking can we have an array that contains only final element.
Adam Czysciak
Ranch Hand
Joined: Feb 25, 2005
Posts: 82
posted
0
Originally posted by ashok_kumar: i want to have final array i.e the elements in that array should be final is it possible
I hope it clarifies: a) You can declare array containing only final elements. b) You can't force the array to contain only final elements (eg as function's parameter) c) You can't force array to disable changing it's elements by arr[idx] = val
To get to grips with this, remember that an array is an instance of the class Array. So if you declare "final int[] = new int[5];" what you're really doing is creating a final instance of class Array. As you know you can change the instance members of a final Object instance but not the reference to the Object, therefore you can change the content of a final array but not assign a different array to it.
Basically there are two fields in the profile, one for your first name and one for your last name. You are currently using just one of those fields and put an underscore between your names.