• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Changing value of Final variable

 
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! :-)
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Omg Sharma,

Welcome to the ranch. In the future, please start a new topic for a new question. Please do not hijack another topic -- especially one that is for a completely different question, that is 10 years old, and have already been answered.

Your post was moved to a new topic.
(This informational message will self destruct in two days)

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic