File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Parameter Use 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 "Parameter Use" Watch "Parameter Use" New topic
Author

Parameter Use

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

I am studying for the SCJP. Please take the following code snippet.



Now I have no problem with lines I have commented, as it makes sense to me. What does not make sense to me is why d[0]++ or c[0]++ works.

c[0]++ --> seems to me this changes the value of final int[] c

since arrays are passed as reference d[0]++ seems like it changes the value of final int[] d = {3}; in the main method.

Is it only the memory address pointing to the array object that cannot be changed or reassigned? For some reason I figured that the values within the array were also unchangeable.

Can somebody please clarify for me?

Thanks,


[How To Ask Questions][Read before you PM me]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Bill Gorder wrote:
Is it only the memory address pointing to the array object that cannot be changed or reassigned? For some reason I figured that the values within the array were also unchangeable.


No. When declaring a final variable, only the variable is not changeable. And in your example, the variable is a array reference. The reference can't be changed, but the array can be changed.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

OK thanks for clearing that up for me Henry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parameter Use