• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

arrays & final

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to have final array i.e the elements in that array should be final is it possible
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 90
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Object[] reference is NOT of type Array.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
no wonder he is so sad, he hasn't seen this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic