Creating java method arguments as final

Who cares what final does between the code below. Is there any advantage to declaring arguments as final .

 public String changeTimezone( Timestamp stamp, Timezone fTz, Timezone toTz){ return .... } public String changeTimezone(final Timestamp stamp, final Timezone fTz, final Timezone toTz){ return .... } 
+80
java methods arguments final
Nov 12 '10 at 7:38
source share
10 answers

As a parameter of a formal method is a local variable, you can access them from internal anonymous classes only if they are declared as final.

This will save you from declaring another local final variable in the body of the method:

  void m(final int param) { new Thread(new Runnable() { public void run() { System.err.println(param); } }).start(); } 
+109
Nov 12 '10 at 8:13
source share

Extract final word from final keyword

Final parameters

The following example declares the final parameters:

 public void doSomething(final int i, final int j) { // cannot change the value of i or j here... // any change would be visible only inside the method... } 

final is used here to provide two indices i and j will not accidentally reset using the method. This is a convenient way to protect against insidious error that erroneously changes the value of your parameters. Generally speaking, short methods are the best way to protect against this class of errors, but the final parameters may be useful in addition to your coding style.

Note that the final parameters are not the considered part of the signature method and are ignored by the compiler when resolving method calls. Parameters can be declared final (or not), without affecting how the method is overstated.

+37
Nov 12 '10 at 8:27
source share

The final does not allow you to assign a new value to a variable, and this can be useful in detecting typos. Stylistically, you can leave the received parameters unchanged and assign them only to local variables, so final will help to implement this style.

I must admit, I rarely remember to use final for parameters, maybe I should.

 public int example(final int basicRate){ int discountRate; discountRate = basicRate - 10; // ... lots of code here if ( isGoldCustomer ) { basicRate--; // typo, we intended to say discountRate--, final catches this } // ... more code here return discountRate; } 
+22
Nov 12 '10 at 7:45
source share

It does not really matter. It just means that you cannot write:

 stamp = null; fTz = new ...; 

but you can still write:

 stamp.setXXX(...); fTz.setXXX(...); 

Basically, this is a hint for the maintenance programmer who follows you that you are not going to assign a new value to a parameter somewhere in the middle of your method, where this is not obvious and can cause confusion.

+14
Nov 12 2018-10-12T00:
source share

The final keyword used for parameters / variables in Java designates the link as final. If the object is transferred to another method, the system creates a copy of the reference variable and passes it to the method. By marking new final links, you protect them from being redirected. Sometimes he considered good coding practice.

+3
Nov 12 '10 at 8:16
source share

For the body of this method, the final keyword will prevent accidental reassignment of argument references, giving a compilation error in these cases (most IDEs will complain right away). Some may argue that using final in the general case, when possible, will speed up the process, but this is not the case in recent JVMs.

+2
Nov 12 2018-10-12T00:
source share

Its just a Java construct to help you define a contract and stick to it. A similar discussion is here: http://c2.com/cgi/wiki?JavaFinalConsideredEvil

BTW - (as twiki says), designating args as final is usually redundant if you follow good programming principles and follow the hass reissign / redefine link to the incoming arguments.

In the worst case, if you redefine the args link, this will not affect the actual value passed to the function, since only the message is transmitted.

0
Nov 12 2018-10-12T00:
source share

I am talking about marking variables and final fields in general - not just applying to method arguments. (Labeling methods / final classes are completely different).

This is a service for readers / future maintainers of your code. Together with a reasonable variable name, it is useful and encouraging for the reader of your code to see / understand what the variables in question are - and it is encouraging for the reader that whenever you see a variable in the same scope, the meaning remains the same thing, therefore (a) he should not scratch his head in order to always understand what a variable means in every context. We have seen too many abuses regarding the "reuse" of variables, which makes it difficult to understand even a short piece of code.

0
Nov 12 '10 at 9:39
source share

- In the past (before Java 8 :-))

The explicit use of the final keyword has affected the accessibility of the method variable for internal anonymous classes.

- In modern (Java 8+) language there is no need for such use:

Java introduced "effectively final" variables. Local variables and method parameters are considered final if the code does not involve changing the value of the variable. Therefore, if you see such a keyword in Java 8+, you can assume that it is not needed. Introducing "effectively final" makes us type less when using lambdas.

0
Jul 29 '19 at 8:29
source share

The final keyword does not allow you to assign a new value to the parameter. I would like to explain this with a simple example

Suppose we have a method

method1 () {

Date dateOfBirth = new date ("1/1/2009");

method2 (DateOfBirth);

method3 (DateOfBirth); }

public mehod2 (Date dateOfBirth) {
....
....
....
}

public mehod2 (Date dateOfBirth) {
....
....
....
}

In the above case, if "dateOfBirth" is assigned a new value in method2, this will lead to incorrect output of method3. Since the value that is passed to method3 is not what it was before passing to method2. Therefore, to avoid this keyword final is used for parameters.

And this is also one of the best practices of Java Coding.

-3
Nov 12 '10 at 9:53
source share



All Articles