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 Changing value of Final variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Changing value of Final variable" Watch "Changing value of Final variable" New topic
Author

Changing value of Final variable

Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
This is one of the review question in Khalid Mughal.

Question:

Which statements would cause a compilation error if inserted in the location indicated in the following program?

public class ParameterUse {
static void main(String[] args) {
int a = 0;
final int b = 1;
int[] c = { 2 };
final int[] d = { 3 };
useArgs(a, b, c, d);
}

static void useArgs(final int a, int b, final int[] c, int[] d) {
// INSERT STATEMENT HERE.
}
}

Select the two correct answers.

a) a++;

b) b++;

c) b = a;

d) c[0]++;

e) d[0]++;

f) c = d;

The answer is a and f. I am wondering why it is not d.

Here is the explanation in the book for the answers however i am not convinced. Can anyone please help me here.

Values can only be assigned once to final variables. A final formal parameter is assigned the value of the actual parameter at method invocation. Within the method body, it is illegal to reassign or modify the value of a final parameter. This causes a++ and c = d to fail. Whether the actual parameter is final does not constrain the client that invoked the method, since the actual parameter values are copied to the formal parameters.


Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Read question again. What would cause compiler error if inserted.

a and f would cause compilation error.

a is final can't be incremented

f because c array is final.


Naseem
[ July 21, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
Exactly, If C is final then how can it be incremented and not cause a compilation error? i.e. c[0]++ (option d).

Kindly answer.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Atul Savant:
Exactly, If C is final then how can it be incremented and not cause a compilation error? i.e. c[0]++ (option d).


In this code, c is not being incremented; you can't increment an array. One of the array elements is being incremented. This is always OK, because there's no such thing as an array with final elements. A final variable referring to an array means you can't make the variable point to another array; you can change the array elements' values, however.


[Jess in Action][AskingGoodQuestions]
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
Hi,

Here the reference is final.so u can't assign new reference it.But u can change the object it points to.That's why we are able to increments it's elements.

Thanks
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
But u can change the object it points to

You can't change the object a final reference points to. That's why c = d gives compiler error.


SCJP 1.4 85%
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
Thanks! :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing value of Final variable