【发布时间】:2015-04-12 02:23:07
【问题描述】:
我正在尝试从服务器类访问客户端类中的变量“名称”。这是我试图做的,但是当我打印它时,它说它是空的(?)。我怎样才能解决这个问题?提前谢谢...
客户端类
class Client {
static Socket socket;
static String name;
public static void main(String[] args) throws Exception {
String hostname = "";
int port = 0;
if (args.length == 3) {
hostname = args[0];
port = Integer.parseInt(args[1]);
name = args[2];
System.out.println("(" + hostname + ":" + port + ")");
} else {
hostname = "localhost";
port = 3000;
name = "default";
System.out.println("Using all default arguments. (" + hostname + ":" + port + ")");
}
Client c = new Client(hostname, port);
c.connect();
}
}
服务器类
class Server {
ServerSocket main;
Helper helper;
static class MailServer extends Thread {
DataInputStream dataIn;
DataOutputStream dataOut;
int index;
Helper helper;
String name = Client.name; //<--- HERE
}
}
【问题讨论】:
-
大概客户端和服务器在不同的 JVM 中运行......或者我在这里遗漏了一些东西。
标签: java class variables static