This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final arrays and initialisation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final arrays and initialisation" Watch "Final arrays and initialisation" New topic
Author

Final arrays and initialisation

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

If I have the following:

final int[];

will all the cells in the array have their value set at 0(int's default value)
or is the final modifier just to prevent reassigning the reference??


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Duran Harris wrote:If I have the following:

final int[];

will all the cells in the array have their value set at 0(int's default value)
or is the final modifier just to prevent reassigning the reference??


assigning values is a runtime thing
and
reassigning check for final's is compile time thing

here you are declaring an array reference that will point to group of int's
the values of these int's can be changed but the array reference will continue to point to this group of int's till its lifetime.


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Let me give you an example of what Sandeep just said



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Ok good..

then array+final behaves exactly the same as:
object+final

Thank you
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
with final you can not do changes. other example with variables see:


This code show error, when you try to compile. because I can not change one value of one variable final, it is impossible.


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

You would probably laugh at this but when I started programming I tried to do this :lol:

int i = 5++;
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

In that case there is still hope for me.
 
GeeCON Prague 2014
 
subject: Final arrays and initialisation