问题
Going through a tutorial for android (related to multithreading, loopers and handlers), i came across this:
public synchronized void enqueueDownload(final DownloadTask task)
My questions are:
- When and why is it needed to declare a parameter of a function as final?
- Is it specific to Java or does something similar exist in other languages such as C/C++?
回答1:
In Java this is usually so that you can access the parameter within an anonymous inner class - which is often used in Android for event handlers and the like.
The real meaning is that the parameter's value cannot be changed within the method, but the purpose is usually for 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
}
回答2:
In your context the final keyword on that parameter means that the variable task can't be reassigned inside the method body, and it is specified for security reasons .
A similar behaviour can be achieved in C++ with a constant function parameters :
If you are writing a function and you do not intend to modify a parameter, you can declare that it is a constant reference parameter.
回答3:
The final keyword is used in several different contexts as a modifier meaning that what it modifies cannot be changed in some sense.
final classes
You will notice that a number of the classes in Java library are declared final, e.g.
public final class String This means this class will not be subclassed, and informs the compiler that it can perform certain optimizations it otherwise could not. It also provides some benefit in regard to security and thread safety.
The compiler will not let you subclass any class that is declared final. You probably won't want or need to declare your own classes final though.
final methods
You can also declare that methods are final. A method that is declared final cannot be overridden in a subclass. The syntax is simple, just put the keyword final after the access specifier and before the return type like this:
public final String convertCurrency()
final fields
You may also declare fields to be final. This is not the same thing as declaring a method or class to be final. When a field is declared final, it is a constant which will not and cannot change. It can be set once (for instance when the object is constructed, but it cannot be changed after that.) Attempts to change it will generate either a compile-time error or an exception (depending on how sneaky the attempt is).
Fields that are both final, static, and public are effectively named constants. For instance a physics program might 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 though it's 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 method arguments are final. This means that the method will not directly change them. Since all arguments are passed by value, this isn't absolutely required, but it's occasionally helpful.
回答4:
You declare something as final if you know that it should never be reassigned. You often want to do this to method parameters, since it rarely makes sense 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 can't claim to know the specifics offhand.
来源:https://stackoverflow.com/questions/8187859/declaring-parameter-of-a-function-as-final-why-and-when-is-it-needed