Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing value of Final variable

 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic