简体   繁体   中英

Instance variable scope in java

Hello good day to all.

I am creating a networking program in java for my Internet Cafe. I have this code for my server

    import java.io.*;
    import java.net.Socket;
    import java.net.ServerSocket;

public class Server
{
    private String name;
    private int id;

    private Socket socket = null;
    private ServerSocket serversocket = null;
    private ObjectInputStream ois = null;

    public Server()
    {
        new Thread(receive).start();

        while(true)
        {
            try
            {
                serversocket = new ServerSocket(4445);
                socket = serversocket.accept();
                System.out.println("Connected..");
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }

    }

    Runnable send = new Runnable(){
        public void run ()
        {

        }

    };

    Runnable receive = new Runnable(){
        public void run ()
        {
            while(true)
            {
                try
                {
                    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                    User user = (User) ois.readObject();
                    System.out.println(user);
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
                catch(ClassNotFoundException e)
                {
                    e.printStackTrace();
                }
            }
        }
    };

    public static void main(String [] args)
    {
        new Server();
    }
}

My problem is that I cant access my instance variable inside my Runnable interface code.

For example the "socket" instance. is not accessible inside this code

Runnable receive = new Runnable(){
        public void run ()
        {
            while(true)
            {
                try
                {
                    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                    User user = (User) ois.readObject();
                    System.out.println(user);
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
                catch(ClassNotFoundException e)
                {
                    e.printStackTrace();
                }
            }
        }
    };

What is the best approach on this and why is not accessible inside that code?

Why? Here is the answer: Why are only final variables accessible in anonymous class?

If you need access to your socket from your server you should declare as a final varible your Socket socket on your Server class.

Try this

public Server() {
    try {
        serversocket = new ServerSocket(4445);
        while (true) {
            socket = serversocket.accept();
            new Thread(receive).start();
            System.out.println("Connected..");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

If I recall correctly, you need to make the socket field final. The reason escapes me right now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM