Question

I recently saw a method like

public void display(final String toDisplay){

}

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top