What is the purpose having final keyword in method parameter? [duplicate]

StackOverflow https://stackoverflow.com/questions/22776423

  •  25-06-2023
  •  | 
  •  

문제

I recently saw a method like

public void display(final String toDisplay){

}

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

도움이 되었습니까?

해결책

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.

다른 팁

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.

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top