从另一个类访问静态变量

时间:2014-02-15 19:43:39

标签: java

我的问题很简单:我需要从另一个类访问变量history(在类BinaryServer中声明)。我使用更多的类来运行这个代码。它只是简单的客户端和服务器由套接字构成.Client向服务器发送二进制代码/文本,服务器将其转换为文本/二进制代码并将其发送回客户端。如果需要,我可以提供所有类。

BinaryServer类

import java.net.*;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import graphics.gui;

public class BinaryServer extends gui implements ActionListener,Runnable
{
private ServerSocket server;
private Socket client;
public String text;
private BufferedReader reader;
public static ArrayList<String> history;


public static String binary_letter;
public static String[] letter;
public static int i;
public static String[] binary;
public static String sendback;


public static void main(String[] args)throws IOException
{
   BinaryServer instance=new BinaryServer();

   gui.buildframe(310,360,"Binary translator server");
   gui.buildpane(300,300,true);
   gui.buildbutton(300,20,"Translate");
   instance.server(63400);
}

public void server(int port)throws IOException
{
    history=new ArrayList<String>(100);
    server=new ServerSocket(port);
    button.addActionListener(this);

    while(true)
    {
        client=server.accept();
        reader=new BufferedReader(new InputStreamReader(client.getInputStream()));
        text=reader.readLine();
        history.add(text);
        message.setText(message.getText()+"\n"+text+": ");
    }
}

@Override
public void actionPerformed(ActionEvent e)
{
    Thread response=new Thread(new BinaryServer());

    if(text.contains("0"))
    {
        int length=text.length();
        letter=new String[length+1];
        sendback="";
        int begin=-8;
        int end=0;

        for(i=1;i<=length/8;i++)
        {
            begin=begin+8;
            end=i*8;
            binary_letter=text.substring(begin,end);
            Libary.translate();
            message.setText(message.getText()+letter[i]);
            sendback=sendback+letter[0+i];
        }
    }
    else
    {
        int length=text.length();
        letter=new String[length+1];
        binary=new String[length+1];
        sendback="";

        for(i=1;i<=length;i++)
        {
            letter[i]=text.substring(i-1,i);
            Libary.encode();
            message.setText(message.getText()+binary[i]);
            sendback=sendback+binary[0+i];
        }
    }
    response.start();
}

public void run()
{
    try
    {
    Socket feedback=new Socket("localhost",63403);

    PrintWriter writer=new PrintWriter(feedback.getOutputStream(),true);
    writer.println(sendback);
    feedback.close();
    return;
    }
    catch(IOException exc)
    {
        System.out.println("");
    }
}
}

BinaryHistory类(我想要访问变量的那个)

public class BinaryHistory
{
    public static void main(String[] args) 
    {
        show();
    }
    public static void show()
    {
        System.out.println(BinaryServer.history);
}

当我从类BinaryHistory访问变量history时,它总是为空。

1 个答案:

答案 0 :(得分:1)

如果您只声明该变量,无论其类型或是否为静态变量,都会获得a default value of null

您还必须初始化变量:

public static ArrayList<String> history = new ArrayList<>();