1

这就是我想要实现的目标

public class UDPThread extends Thread {

    int port;
    Spb_server station = null;


    public UDPThread(int port, Spb_server station) {
        this.port = port;
        this.station = station;
    }   

    public static void main(String[] args) {
        new UDPThread(5002,station).start();
    }  
}         

station是我正在创建的类的对象Spb_server,我想在main方法中访问它。但它随后要求我制作static我不想做的修饰符。有什么办法可以做到这一点?

4

2 回答 2

0

好吧,你必须初始化Spb_server某个地方。在这段代码中,在主函数中这样做是有意义的,所以像

public static void main(String[] args) {
    Spb_server station = new Spb_server() // Or whatever your constructor is
    new UDPThread(5002,station).start();
}
于 2013-11-07T04:38:28.333 回答
0

如果要从 main 访问,则必须将其设为静态。两种可能:

int port;
static Spb_server station = null;


public UDPThread(int port, Spb_server station)
{
    this.port = port;
    this.station = station;
}   

public static void main(String[] args)
{
    new UDPThread(5002,station).start();
}

或局部变量:

int port;



public UDPThread(int port, Spb_server station)
{
    this.port = port;
    this.station = station;
}   

public static void main(String[] args)
{
    Spb_server station = null;
    new UDPThread(5002,station).start();
}
于 2013-11-07T01:48:57.157 回答