Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing static variable from another class

Tags:

java

My problem is simple: I need to access variable history (which is declared in class BinaryServer) from another class.I'm using more classes to run this code.It's just simple client and server made of sockets.Client sends to server binary code/text and server translates it to text/binary code and sends it back to client.I can provide all classes if needed.

BinaryServer class

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 class (The one I want access variable from)

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

When I access variable history from class BinaryHistory, it's alway null.

like image 318
John Smith Avatar asked Apr 25 '26 23:04

John Smith


1 Answers

If you only declare the variable, regardless of the type or whether or not it's static, it will get a default value of null.

You have to initialize the variable too:

public static ArrayList<String> history = new ArrayList<>();
like image 97
Makoto Avatar answered Apr 27 '26 13:04

Makoto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!