This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Regarding the FINAL qualifier and arrays... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding the FINAL qualifier and arrays..." Watch "Regarding the FINAL qualifier and arrays..." New topic
Author

Regarding the FINAL qualifier and arrays...

Mike A. Lester
Greenhorn

Joined: Dec 26, 2005
Posts: 7
Hi all,
FACT I..
When we use a FINAL qualifier for a variable, once the variable is initialized, u can not re-initilize it.

FACT II
An array of objects stores just the references to actual objects.

let me explain the scenario with an example;

I am having a class sheep. and following is my code:

sheep obj_sheep_1;
final sheep[] array_of_sheep; //FINAL array, can not be reinitialized

obj_sheep_1 = new sheep();

array_of_sheep = new sheep[2];
array_of_sheep[0] = new sheep();
array_of_sheep[1] = obj_sheep_1;

Now, my understanding is, elements (rathar indices) of array_of_sheep will hold just references to the sheep object. I.e. array_of_sheep[0] will hold a reference to a newly created sheep object and array_of_sheep[1] will hold a reference to an already created sheep object identified by the name obj_sheep_1. This can be confirmed by modifying properties of obj_sheep_1 and then checking that the changes in these properties are reflected in array_of_sheep[1].
Now, since the array is "final" and it has been initialized, we can not change the references stored in its indices.
Now, what I have observed is, after this, if I delete the obj_sheep_1 object
using

obj_sheep_1 = null;

the array_of_sheep[1] continues to hold a copy of it....

Can someone explain me this functionality??

Thanks
Aliester
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You seem to be under the misapprehension that you can use "final" to declare an unmodifiable array. You can't. There is no way in Java to make an array unmodifiable.

The "final" keyword declares an object reference to be unmodifiable. If the type of the object reference is an array, then you cannot change that object reference to point to a different array. Nothing in this stops you from changing the contents of the array.

Unmodifiable Collections are possible, by the way. See Collections.unmodifiableSet() etc.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Mike A. Lester
Greenhorn

Joined: Dec 26, 2005
Posts: 7
I guess I didn't phrased my question rightly.
I know that when I declare an array of objects as final, I CAN modify the contents of the objects reffered by the array but I CAN NOT change the references. I.e. I can't make some array index to point to some different object once the array is initialised.
The question is different.
An array objects just stores the reference to the objects.
So, modifying the objects, (please see the code in my first message) will reflect the changes in the array as weel as BOTH ARE REFFERING TO SAME MEMORY.
What will happen when object is destroyed??
e.g.
Assume my class is Animals
Code is below:

Animals animal1; //an object
final Animals[] array_of_ans // an array of objects

animal1 = new Animals(); //create the object.
array_of_ans = new Animals[2] // INITIALIZE the array. No refs assigned.
array_of_ans[0] = new Animals(); //Asign reference of a new object to index
array_of_ans[1] = animal1;

animal1 = null;
I have observerd that array_of_ans[1] after deleting setting animal1 to null, still array_of_ans[1] keep on holding an object of class Animals the content of which are same as animal1.

How this is done?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Please reread what Peter posted. Reference array_of_ans is final. You can't reassign to it later:But the references held in the array are not final (and there is no way to make them final) so they can be freely reassigned, as the above code demonstrates. But don't believe me -- test your assumptions, compile the code.


There is no emoticon for what I am feeling!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Animals animal1; //an object
final Animals[] array_of_ans // an array of objects

animal1 = new Animals(); //create the object.
array_of_ans = new Animals[2] // INITIALIZE the array. No refs assigned.
array_of_ans[0] = new Animals(); //Asign reference of a new object to index
array_of_ans[1] = animal1;

animal1 = null;
The "animal1" variable is not an object. It is a variable that contains a reference to an object. It looks to me like you understand that, nearly, from other parts of your post.

So, you create an Animals object and assign a reference to it to the "animal1" variable. Then you create another Animals object and assign a reference to it to element 0 of the array. And then you assign a reference to the first Animals object object to element 1 of the array.

At this point, there are two references to the first Animals object.

Then you set the "animals1" variable to null. This means that it doesn't have a reference to the first Animals object any more. However you ask "What will happen when object is destroyed??" which suggests you think that an object is being destroyed somehow. That isn't the case. Don't confuse a variable with an object it is referring to. Just because one variable stops referring to an object, that doesn't prevent another variable (or array element) from continuing to refer to the object.

To repeat an example I saw elsewhere on this forum, suppose you and I are pointing at a pencil on my desk. Then I stop pointing at the pencil. Are you still pointing at the pencil?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Aliester Moody:
I guess I didn't phrased my question rightly.
I know that when I declare an array of objects as final, I CAN modify the contents of the objects reffered by the array but I CAN NOT change the references.

This is incorrect. You CAN change the references in each element of the array. The final keyword only prohibits you from changing the reference to the whole array to point to a different array:


I think this is exactly what everyone else has said so far. I just wanted to make sure it was clear with an example.

Layne


Java API Documentation
The Java Tutorial
Mike A. Lester
Greenhorn

Joined: Dec 26, 2005
Posts: 7
Hi Paul Clapham,
U r right.
Now I am understanding the concept and also why Garbage Collector is required..
The situation in the code is ad below:
There is one object, i.e. one chunk of memory holding the data, and two variables say Var1 and Var2 are referencing to it..
When I say Var1 = null what I am doing is not deleting the object from memory but just removing the reference vetween Var1 and the memory holding the actual Object.
Since this memory or this actual object is being reffered by Var2, it will be intact..
the moment I assign Var2 = null, the memory will be cleared and all object data will be removed by the Garbage collector...
Am I right this time??

thanks a ton.... :-))
Aliester.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Once an object has no references it is marked for garbage collection, it is not necessarily deleted right then, just whenever the GC gets around to it.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding the FINAL qualifier and arrays...
 
Similar Threads
Sun Cirtification
Understanding Multi-dimension Arrays
Array declaration
doubt in Array construction
arrays