2

The use of Final keyword inside the method argument puzzles me

public void helloWorld(String str)
{

}


public void helloWorld(Final String str)
{

}

What is the differences between them?
BelieveToLive
  • 281
  • 1
  • 4
  • 18

1 Answers1

4

public void helloWorld(final String str) doesn't allow you to change the value of str locally inside the method, while without the final keyword, you can change it locally.

public void helloWorld(String str)
{
     str = "something"; // OK
}


public void helloWorld(final String str)
{
    str = "something"; // ERROR
}

Of course, even helloWorld(String str) can't change the value of the String passed to the method, since Java is a pass-by-value language.

Eran
  • 387,369
  • 54
  • 702
  • 768