Java and the use of the Final keyword

I am new to Java, programmed in Delphi and C # for some time. My question is related to the use of the keyword "final" for a variable that contains an instance class when the variable declaration and instance occur within the same method. eg

private String getDeviceID() { //get the android device id final TelephonyManager tm = (TelephonyManager)GetBaseContext().getSystemService(Context.TELEPHONY_SERVICE); final String deviceID = tm.getDeviceId(); // log debug message containing device ID Log.d(LOG_CAT, "getDeviceID: " + deviceID); return deviceID; } 

okay, so I think that I get the fact that the “final” variables can only be assigned once and cannot be changed due to the “final” keyword for each declaration, but both variables do not go beyond when does the method go out? and the method call again simply redistributes the two new final variables that once again go out of scope when the method exits?

Does it seem strange to use the keyword "final" for these variables? if I do not understand how they affect local variables within the scope of the method?

Can someone enlighten me on what influence the “final” has to do with the scope of the method, or declares these specific variables as the final, just stupid thing that someone did?

+4
source share
8 answers

final does not affect the scope.
It just prevents the variable from being reset.

It serves as a signal to other developers that these variables will never change, and this prevents you from accidentally changing them.
This is especially useful when using longer methods.

final also required to use a variable in an anonymous inner class, since Java does not support true closures.

+6
source

These variables will indeed collect garbage as soon as the GC runs when the method completes.

The final keyword is really present as a hint: this variable is created once, you should not touch it in the body of the method itself. Similarly, declaring the final method parameters prohibits their reuse (which, imho, is a good thing).

Please note that the keyword only affects the link to the object: this does not mean that the methods in this link to the object that change its internal state will stop working (typical example: setters).

Another note: when you omit the final keyword and you do not change the variable in the body of your method, the JVM is smart enough to optimize this case. So you can omit this. Regardless of whether you use it and where you use it, it is a matter of taste / coding style.

And finally, it is good practice to declare public static variables as final: otherwise everything can change it! Think string constants etc.

+5
source

There is nothing special about a local variable or final parameter.

Declaring a local variable or parameter as final really not very much and rarely necessary. There are two reasons for this:

  • Some developers believe that everything that does not need to be changed should be unchanged. Although I agree in principle (immutability is a good thing in many ways), I think that for a language like Java, the declaration of all final goes overboard.
  • If your method contains a local or anonymous inner class and you want any of its local variables or parameters to be available for code in the inner class, you must declare them final . This is a kluge in the Java language; its purpose is to prevent code in the inner class from trying to change variables or parameters after they are no longer alive.
+1
source

They are final in the area in which they are located. This is how it works.

I suggest that the goal of making them final in this case is to prevent future developers from changing them when they should not be changed. This is just security coding in this case.

0
source

final is similar to const in c for primitives and references.

0
source

readability


When it comes to the local area, I find its use is a variable. That is, some programmers prefer to use it (and abuse it ... like me), and some will use it sparingly (for example, to ensure that a class cannot be a subclass, immutability, etc.).

I find that when working with a development team, this is either all or nothing. After you enable final modifiers in the local area, you know that you are connected, and not so much your team can do (except that you are fired ... somehow ... in order to be effective by the developer).

0
source

My perspective

It is good practice (for better service, etc.) to make local variables final. This is one way to reduce side effects . Free code with a side effect is easy to reason about and therefore more readable and easy to maintain.

0
source

Too religious programmers would tell you to mark final local variables (including method parameters) when you can.

In practice, no one does this, including those programmers.

Do not worry.

0
source

Source: https://habr.com/ru/post/1388543/


All Articles