Solved

final parameter

Posted on 2012-03-15
3
236 Views
Last Modified: 2012-03-16
Does declaring a parameter as final serve a purpose?

public void myMethod (final int x) {
   ....
}

Obviously, x is already passed by value, so the value can't be changed.
If I understand correctly, I copy of x is made for use in the method, and if its not already final by default, declaring it so might make the copy take less space.
0
Comment
Question by:allelopath
3 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 167 total points
ID: 37724716
http://www.jguru.com/faq/view.jsp?EID=19556

Says the param won't be modified.
0
 
LVL 7

Accepted Solution

by:
gudii9 earned 167 total points
ID: 37724978
Sometimes its nice to be explicit(for readability) that the variable doesn't change. Here's a simple example where using final can save some possible headaches

public void setTest(String test) {
    test = test;
}

if you forget the 'this' keyword on a setter the variable you want to set doesn't get set. However if you used the final keyword on the parameter then the bug would be caught at compile time.



http://stackoverflow.com/questions/500508/why-should-i-use-the-keyword-final-on-a-method-parameter-in-java
http://stackoverflow.com/questions/316352/why-would-one-mark-local-variables-and-method-parameters-as-final-in-java
0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 166 total points
ID: 37724980
it cannot be modified for the calling method, but it can be modified in the method in which it is going as a parameter. Making it 'final' make it constant in a method in which it has gone as a parameter also.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is Applet the way to go for my drag and drop system? 8 25
java imports not found 4 29
java mysql insert application 14 29
Tagging and Merging on Branch 1 30
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

830 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