Access a static variable from another class

My problem is simple: I need to access a variable history(which is declared in the BinaryServer class) from another class. I use more classes to run this code. This is just a simple client and server from .Client sockets sends binary code / text to the server, and the server translates it into text / binary code and sends it back to the client. I can provide all classes if necessary.

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

Class BinaryHistory (the one with which I want to get the access variable)

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

When I access a variable historyfrom the BinaryHistory class, it is always null.

+3
source share
1 answer

, , null.

:

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

All Articles