Solved

final parameter

Posted on 2012-03-15
3
233 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
@SBGen Method 3 36
starter POM and spring-boot-starter,  spring-boot-web 2 43
Java JRE greater than 1.6 5 50
jar file executable 12 40
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

910 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

18 Experts available now in Live!

Get 1:1 Help Now