aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes arrays & final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "arrays & final" Watch "arrays & final" New topic
Author

arrays & final

ashok kumar
Ranch Hand

Joined: Feb 28, 2005
Posts: 30
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
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
You cannot add or remove objects from an array.
You can reassign an element of the array, which may be a reference type.


Tony Morris
Java Q&A (FAQ, Trivia)
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
let the code speak!


[ 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
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
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


Adam<br /> <br /><i>SCJA, SCJP 5.0, SCWCD 1.4, SCBCD 5, SCEA 5</i>
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.


42
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
A Object[] reference is NOT of type Array.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrays & final
 
Similar Threads
Doubt About Object Reference Casting Rule
Final arrays and initialisation
final array
String doubt
const syntax.