Solved

method argument as final

Posted on 2016-08-14
1
107 Views
Last Modified: 2016-08-14
Hi,

When do we need to declare method argument as final say as below

void addPerson(final Employee theEmployee) {
......
.....

}

please advise
0
Comment
Question by:gudii9
1 Comment
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 41755845
>> When do we need to declare method argument as final say as below

You almost never have to declare arguments as final, but some people consider it good practice and it may be adopted as a coding standard by some companies.

The reason to do this is that "final" makes sure the parameter cannot be modified within the method, which keeps the logic simpler.

void addPerson(final Employee theEmployee) {
    ... stuff before
    process(theEmployee) ; // guaranteed that this is the employee that was passed in
}

Open in new window


Without final the value could change:
void addPerson(Employee theEmployee) {
    ... stuff before
    process(theEmployee) ; // maybe modified along the way
}

Open in new window


E.g. Here's how not making it final allows parameter to be changed:
void addPerson(Employee theEmployee) {
    ... stuff before
    theEmployee = anotherEmployee; 
    ... more stuff
    process(theEmployee) ; // may not be what was intended
}

Open in new window


So it's probably a good idea to usually add "final" to parameters to methods by default.
So why don't people do this more often?  Because it's extra typing.

Doug
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 25
Vector, list Questions R 6 32
spring maven example issues 3 10
restrict decimal places for double datatype 10 22
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question