• Post Reply Bookmark Topic Watch Topic
  • New Topic

Marking a parameter as final  RSS feed

 
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
Can anyone explain to me that why should we mark a parameter as final
For example:



The second parameter lastName is marked as final

Regards!
 
Ranch Hand
Posts: 200
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We should mark any variable as final so that once its value is assigned that it can't be changed.
references->http://www.geeksforgeeks.org/g-fact-24-finalfinally-and-finalize-in-java/
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O Shea wrote:We should mark any variable as final so that once its value is assigned that it can't be changed.
references->http://www.geeksforgeeks.org/g-fact-24-finalfinally-and-finalize-in-java/


Thanks guy, but my question is about final parameter, which is appeared in defining method like the code that I posted first in this thread.
 
O Shea
Ranch Hand
Posts: 200
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes also by making parameter as final allows you to warn you when you accidentally try to modify a value, hence better optimization of the class file.
 
O Shea
Ranch Hand
Posts: 200
3
Java MySQL Database Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you don't understand
see what is method parameter-> It is also a local variable since it can be used only inside that method only.
See->

Here lastName is method parameter which is a local variable since its scope is only inside that getSortOrder() method only.
So what you are doing is that making a local variable as final, so making any variable whether it is local, static or instance as final ensures that that variable can be assigned value only once.

See example->



Hope now it would help
 
Sheriff
Posts: 4935
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how well indented and formatted code supposed to be looking like (version of given code above) :
 
Master Rancher
Posts: 2046
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose your parameter is of an Immutable type, then the qualifier 'final' does not make any difference at all.

Suppose that the parameter is of a mutable type. Does it matter if the parameter is final or not? How would you investigate this?
 
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:This is how well indented and formatted code supposed to be looking like (version of given code above) :

I also indent my classes as follows:

but isn't this a matter of taste?
 
Liutauras Vilda
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is, but given code example, which i fixed, contained inconsistent braces style + it had incorrect indentation and some other issues, so it wasn't in particular about braces.

Both braces placing styles are good as long as one of them is consistent throughout the source file, but it wasn't the case, so i chose the one i prefer.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!