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.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Nick Div
  • 5,338
  • 12
  • 65
  • 127

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,356
  • 19
  • 50
  • 61
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • 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,091
  • 1
  • 17
  • 32
1

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

Vincent Cantin
  • 16,192
  • 2
  • 35
  • 57
0

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

deiga
  • 1,587
  • 1
  • 13
  • 32