I have already met more than once, but I just thought ... And what does such a construction mean, when in the method one of the parameters is allowed has the access modifier final ...

Indeed, in any case, when you call the method again, you can pass a new value to it ...

  • one
    With a new call, you can, but inside the method itself you can’t change it - rjhdby
  • In short, it will be available to the "lower levels", if they do not have more final'ov with the same variable name. - nick
  • From the comments you can understand that you are really interested in the answer to this question ? - pavlofff

5 answers 5

  1. If the final parameter is specified when a variable is declared, it cannot be changed.

     public void i1(final String i1) { i1 = ""; // ошибка компиляции } 
  2. Also, the final parameter gives access to anonymous classes. I know such an example on Android when you launch Activity :

     @Override protected void onCreate(final Bundle i1) { super.onCreate(i1); setContentView(R.layout.layout_i1); new AlertDialog.Builder(this) .setTitle(getString(R.string.app_name)) .setPositiveButton("ОК", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface i2, int i3) { i1.clone(); // Π²ΠΎΡ‚ Ρ‚ΡƒΡ‚ ΠΌΠΎΠΆΠ΅ΠΌ Π΅Π³ΠΎ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ Волько ΠΏΠΎΡ‚ΠΎΠΌΡƒ, Ρ‡Ρ‚ΠΎ Ρƒ Bundle i1 ΡƒΠΊΠ°Π·Π°Π½ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ final return; } }) .setNegativeButton("Π’Ρ‹Ρ…ΠΎΠ΄", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface i1, int i2) { i1.cancel(); finish(); return; } }) .create() .show(); } 

After all, if you replace this line

 protected void onCreate(final Bundle i1) { 

on this

 protected void onCreate(Bundle i1) { 

here it is

 @Override public void onClick(DialogInterface i2, int i3) { i1.clone(); <<<--------- Π² этой строкС return; } 

there will be a compilation error.

  • I set an example on Android, since there is no regular Java at hand. There you can also remember hundreds of such examples. From memory I can be mistaken. - nick
  • one
    It was this example with the use of anonymous classes, what was interesting ... Just did not quite understand about the lower levels ... Why do they cause an error? - Aleksey Timoshchenko
  • About the lower levels, I just put it that way. These are subclasses. That is, for example, for example, a class in a class. I will edit the question, and, perhaps, it will become clearer. - nick
  • one
    It is best to express yourself in terms of the programming language in which you write the answer (and not "I just put it that way ..") otherwise your answer may not only help but confuse even more - pavlofff
  • one
    Exactly, like anonymous classes. I then remove all this snowstorm about levels. - nick

If the parameter in the method does not have a final modifier, then the variable can be replaced by another object. For example:

 void f(String x){ x = "New"; System.out.println(x); //"New" } 

The final modifier of the parameter excludes this possibility.

 void f(final String x){ x = "New"; // ошибка компиляции } 

    In a prototype of a method, arguments that have a final modifier cannot be overridden in the body of the method. In general, the final modifier tells the compiler to immediately know that a variable, method or class is read-only. Final variables are not initialized by default, they must be explicitly assigned a value when they are declared or in the constructor, otherwise a compilation error.

      This is to ensure that during the execution of the method, the value of the argument was not redefined anywhere, for example.

        public static String method(final String city) { city = "Moscow"; //ошибка return city; } 
      • Actually, not only. - nick
      • @ L'Esperanza is interesting to know and for what else? - Artem Konovalov
      • Now I will write the answer. - nick
      • strictly speaking it will work for String-a, but for a changeable object there is no - a link, you really can't change it, but the value of the link is easy - s_klepcha
      • hmm, naturally. I think it's obvious - Artem Konovalov

      For example, what could be done like this:

        void method(final int i) { mMember.setListener(new Listener() { public void call() { System.out.println(i); } }); } 
      • Not always true, in java 8 there was such a thing as effective final. The compiler can guess in which cases the variable is unchanged and it does not need to be prompted with the word final. - Artem Konovalov
      • there is an android tag in question - s_klepcha
      • @Artem Konovalov; In Java 8, yes. How about Java 7? Android differs from Java only in that there are other classes there. - nick
      • in java 8, the code without final will compile, below not - Artem Konovalov