0

I recently saw a method like

public void display(final String toDisplay){

}

I want to know the purpose of final keyword in method parameter.

BTW: I have already searched it on SO, so please don't downvote; I know its a duplicate. I know this question has been asked many times and trust I went through all of them but did not understand any of the answers. Please while answering assume that you are talking to a beginner.

Kevin Panko
  • 8,069
  • 19
  • 50
  • 60
Nick Div
  • 4,904
  • 9
  • 60
  • 116

4 Answers4

4

final variables means you can not change the value.

For example final int x=9; and after that if you change it with x=6; then it will not compile.

So in your case

public void display(final String toDisplay){

}

In this method you are allowing a String argument and after that within that method if you try to change then it will not compile.

Kevin Panko
  • 8,069
  • 19
  • 50
  • 60
SpringLearner
  • 13,546
  • 20
  • 71
  • 113
  • Thanks that was simple enough. – Nick Div Apr 01 '14 at 17:08
  • what is the point of this? any "changes" would be to the locally scoped reference, and not to the actual object, so clients cannot possibly see any difference, right? – sara May 01 '16 at 16:15
2

suppose your method doing complex calculation so by accidentally you may not change the value of toDisplay variable this is the main reason I can say.

Sanjay Rabari
  • 2,055
  • 1
  • 16
  • 29
1

It is to make sure that the implementation will not change the reference.

Vincent Cantin
  • 14,822
  • 2
  • 32
  • 55
0

It simply tells the compiler that this method can not change the thing that parameter points to.

deiga
  • 1,527
  • 12
  • 29