What is the effect of final variable declaration in methods?
Posted
by Finbarr
on Stack Overflow
See other posts from Stack Overflow
or by Finbarr
Published on 2010-05-09T19:39:38Z
Indexed on
2010/05/09
19:48 UTC
Read the original article
Hit count: 206
Classic example of a simple server:
class ThreadPerTaskSocketServer {
public static void main(String[] args) throws IOException {
ServerSocket socket = new ServerSocket(80);
while (true) {
final Socket connection = socket.accept();
Runnable task = new Runnable() {
public void run() {
handleRequest(connection);
}
};
new Thread(task).start();
}
}
}
Why should the Socket
be declared as final
? Is it because the new Thread
that handles the request could refer back to the socket
variable in the method and cause some sort of ConcurrentModificationException
?
© Stack Overflow or respective owner