Solved

method argument as final

Posted on 2016-08-14
1
62 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 26

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now