早些时候,当内部匿名类没有看到“外部”类的字段时,我遇到了问题。我需要制作一个最终变量以使其对内部类可见。现在我有一个相反的情况。在“外部”类“ClientListener”中,我使用内部类“Thread”和“Thread”类,我有“run”方法,并且确实从“outer”类中看到了“earPort”!为什么?
import java.io.IOException;
import java.net.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ClientsListener {
private int earPort;
// Constructor.
public ClientsListener(int earPort) {
this.earPort = earPort;
}
public void starListening() {
Thread inputStreamsGenerator = new Thread() {
public void run() {
System.out.println(earPort);
try {
System.out.println(earPort);
ServerSocket listeningSocket = new ServerSocket(earPort);
Socket serverSideSocket = listeningSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(serverSideSocket.getInputStream()));
} catch (IOException e) {
System.out.println("");
}
}
};
inputStreamsGenerator.start();
}
}