Why my inner class DO see a NON static variable?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-23T11:01:10Z Indexed on 2010/03/23 11:03 UTC
Read the original article Hit count: 290

Filed under:
|
|
|

Earlier I had a problem when an inner anonymous class did not see a field of the "outer" class. I needed to make a final variable to make it visible to the inner class. Now I have an opposite situation. In the "outer" class "ClientListener" I use an inner class "Thread" and the "Thread" class I have the "run" method and does see the "earPort" from the "outer" class! Why?

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();      
    }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about visibility