Declaring a function parameter as final: why and when is it necessary?

After going through the android tutorial (related to multi-threaded, loopback and handlers), I came across this:

public synchronized void enqueueDownload(final DownloadTask task) 

My questions:

  • When and why is it necessary to declare a function parameter as final?
  • Is it specific to Java or does something like that exist in other languages ​​like C / C ++?
+4
source share
4 answers

In Java, this is usually the case so that you can access the parameter inside an anonymous inner class, which is often used in Android for event handlers, etc.

The real value is that the parameter value cannot be changed inside the method, but the goal is usually to anonymous inner classes ...

 public synchronized void enqueueDownload(final DownloadTask task) { SomethingHandler handler = new SomethingHandler() { @Override public void handleSomething() { // This would not compile if task were not final task.doSomething(); } }; // Use handler } 
+10
source

In your context, the last keyword of this parameter means that the task of the variable cannot be reassigned inside the method body, and it is indicated for security reasons.

A similar behavior can be achieved in C ++ with constant function parameters:

If you are writing a function and you are not going to change the parameter, you can declare that it is a constant reference parameter.

+2
source

You declare something as final if you know that it should never be reassigned. You often want to do this with method parameters, since it is rarely advisable to reassign a method parameter.

 void foo(String str) { // no final str = "hijacked"; // perfectly fine } void foo(final String str) { // final str = "hijacked"; // compile error } 

C and C ++ use const instead of final , but I cannot say that you do not know the specifics.

+1
source

The latter keyword is used in several different contexts as a modifier, meaning that what it changes cannot be changed in a sense.

final classes

You will notice that a number of classes in the Java library are declared final, for example.

public final class String This means that this class will not be subclassed and tells the compiler that it can perform certain optimizations, otherwise it could not. It also provides some security and thread safety benefits.

The compiler will not give you a subclass of any class declared final. You probably won't want to or want to declare your own classes final, though.

final methods

You can also declare methods to be final. The method declared final cannot be overridden in a subclass. The syntax is simple, just put the final keyword after the access specifier and before the return type as follows:

 public final String convertCurrency() 

final fields

You can also declare fields final. This is not the same as declaring a method or class final. When a field is declared final, it is a constant that will not change and will not change. It can be installed once (for example, when the object is constructed, but after it it cannot be changed.) Attempts to change it will generate either a compile-time error or an exception (depending on how hidden the attempt is).

Fields that are final, static, and public are actually called constants. For example, a physics program may define Physics.c, the speed of light, as

 public class Physics { public static final double c = 2.998E8; } 

In the SlowCar class, the speedLimit field is likely to be both final and static, although it is private.

 public class SlowCar extends Car { private final static double speedLimit = 112.65408; // kph == 70 mph public SlowCar(String licensePlate, double speed, double maxSpeed, String make, String model, int year, int numberOfPassengers, int numDoors) { super(licensePlate, (speed < speedLimit) ? speed : speedLimit, maxSpeed, make, model, year, numberOfPassengers, numDoors); } public void accelerate(double deltaV) { double speed = this.speed + deltaV; if (speed > this.maxSpeed) { speed = this.maxSpeed; } if (speed > speedLimit) { speed = speedLimit; } if (speed < 0.0) { speed = 0.0; } this.speed = speed; } } 

final arguments

Finally, you can declare that the arguments to the method are final. This means that the method will not directly change them. Since all arguments are passed by value, this is not entirely necessary, but sometimes useful.

+1
source

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


All Articles