• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Final parameter in a method

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method has a final parameter z. Now at 2 I am merely using the value of z, I am
not modifying z. Still there is compiler error.
I could not convince myself for this behavior, for I think that final variable can be assigned value only once, but this value can be used frequently.

Public double x(int y, final double z){
z=z/2;
return y * z;//compiler error//2
}
z has been declared final, and its value can not be modified
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in your example you're assigning value to z twice:
The first time when you call the method and the second time as you write z=z/2;
Try using a tempvariable instead of z to store the intermediate value inside the method.
/Mike
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zeeshan.
Once you get the actual argument in "final parameter z", then you can't change z's value because it is declared as final(immutable).In statement z=z/2; you are trying to chane original value of z or you may say "original argument passed in z"
Hope now it is clear to you.
Regards,
Hassan.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic