• 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

What does this mean?

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the meaning that there is a "final" before a method parameter,such as
public void mymethod( final int i)
{.......}
Does this final here make sense?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the method is called, i is assigend and can not be changed in the method. It is a final for the entire method. Hope this explains it for you.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yes and no... it depends!
Final means that you won't be able to assign anything to i... which doesn't make much sense anyway. Cos even if you're able to assign anything to i (that is, with no final), the result is not propagated back to the caller... so why bother?
Hmm... any comments, anyone?

Originally posted by Bin Zhao:
I don't understand the meaning that there is a "final" before a method parameter,such as
public void mymethod( final int i)
{.......}
Does this final here make sense?


 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If no void is present, does final make any sense?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bin,
Consider a method which has a local class.
Now if u want to access the arg from within the local
class , the arg should be final.
Jeban.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ray,
Your argument is true only for primitive datat types. But for object data types making them final will prevent the original reference getting overwritten by other methods.
To give you an example, let's consider you are writing a framework. Lets say it is very important that you maintain the state information of all the objects intact across method invocations. For this reason, you want to shield your object references from being accidentally or unscrupulously overwritten by seemingly 'good' methods. You can easily achieve this by marking the parameter as final. This will help you keep a check on any code that tries to modify the original referece.
I wrote a small program to drive home my arguments -

Another trivial use of final method parameter is inline-documentation. In the above code, the final parameter suggests to someone reading the code that the method is not expected to change the reference being passed in.
Nevertheless, marking a variable final is not the foolproof way because one can still use mutator methods and change the contents without changing the reference!!.. Or one can extend the class and override the method with non-final signature!
Hope that helps.
Ajith


[This message has been edited by Ajith Kallambella (edited October 09, 2000).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bin,
The Java Programming Language recommends using a final method parameter when you don't intend to change a parameter's value. It makes your intent clear to other's using your class and it helps the compiler optimize expressions in the method which use the parameter.
Hope that helps.
------------------
Jane
 
Bin Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all of your replies from different point of views.
But if the parameter is of reference type,then we may change the object which is being pointed by the parameter,although we can not make it point to other objects. Then this "final" seems having no use?
 
reply
    Bookmark Topic Watch Topic
  • New Topic