• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrays & final

 
ashok kumar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to have final array i.e the elements in that array should be final is it possible
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot add or remove objects from an array.
You can reassign an element of the array, which may be a reference type.
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let the code speak!


[ March 01, 2005: Message edited by: Aruna Agrawal ]
[ March 01, 2005: Message edited by: Aruna Agrawal ]
 
ashok kumar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Object[] reference is NOT of type Array.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ashok_Kumar"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

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.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic