Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should method parameters be made as final in Java8

Tags:

java

In java8, variables are effectively final if we do not assign them again. So, it means if we are declaring a method and if we don't declare its parameters to be final, then they are effectively final if we don't assign them in the method definition. So, does making the parameters final make any difference in java8?

like image 395
Raj Kumar Avatar asked Aug 11 '26 15:08

Raj Kumar


1 Answers

IMO, the reason to mark a parameter final is to show that you are depending on it being final. This might be the case if you are using the parameter in a lambda expression or an inner class. Marking the parameter final tells the next programmer who comes along (or you a year from now) that there is code that relies on that parameter being final.

like image 183
CryptoFool Avatar answered Aug 15 '26 04:08

CryptoFool



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!