• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

final variable declaration

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show the code?
 
Pavan Chillara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My Code is as follows

 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see where you're trying to change the final parameter.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final variables do not act like constants
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic