Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

final variable declaration  RSS feed

 
Pavan Chillara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have declared one of my class variables as final and in the method defined in that class i declared one of my method variables as final.Here i am getting compilation error when i am trying to change the both the variables.
But I have defined one more method in the same class with the method argument as final variable.But it wont be showing any compilation error when i am trying to change the value.

Can anyone explain me the reason why is this happening.

Regards,
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the code?
 
Pavan Chillara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My Code is as follows

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see where you're trying to change the final parameter.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not see any final variable changes.
And what kind of compilation failure are you getting.

final int j = 10;

int k = j + 1;

In this the final variable is not getting changed.
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes it would return a compilation error. The value of j is changed to 21 inside the function A(final int k) and returns the new value and assigns it to the final variable n in method X().

Final variable values cannot be changed. It acts like a constant.

bye for now
sat
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final variables do not act like constants
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satishkumar janakiraman:
The value of j is changed to 21 inside the function A(final int k)


No it's not. The j that you change in A() is a local variable with the same name as the instance variable. Try changingto
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pavan Chillara:
void X() {
final int n = 0; // Statement (1)
System.out.println("This is Example");
while (true) {

n = A(j); // Compilation Error --> Statement (2)
System.out.println("n " + n);
j++;
if (n == 25)
break;
}
}

}

At (1), you definitely assign "n" the value 0, which is a final variable. At (2), you are trying to re-assign a value to "n", a final variable, which already is definitely assigned a value at (1). This is the error in your program; its not possible to change the value of 'n' once you have definitely assigned it a value.
Hope clarified the confusion.

Regards,
Abdul Rehman.
[ November 23, 2006: Message edited by: Abdul Rehman ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!