There is a first class in which I want to transfer data from the second class.

public class Client extends UserInterfaceClient { private PrintWriter writer; Socket socket; public void setUpConnection (){ TestActionListener testActionListener = new TestActionListener(); try { socket = new Socket("127.0.0.1", 5052); writer = new PrintWriter(socket.getOutputStream(),true); writer.println(testActionListener.getMessages()); System.out.print(testActionListener.getMessages()); writer.flush(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 

And here is the second class from which I'm trying to convey the value.

 public class UserInterfaceClient { JTextField outgoing; JTextField test; public void go(){ JFrame frame = new JFrame("Client"); JPanel mainPanel = new JPanel(); outgoing = new JTextField(20); test = new JTextField(10); JButton sendButton = new JButton("Send"); ActionListener actionListener = new TestActionListener(); frame.getContentPane().add(BorderLayout.CENTER, mainPanel); frame.setSize(400, 500); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); mainPanel.add(sendButton); mainPanel.add(outgoing); sendButton.addActionListener(actionListener); } public class TestActionListener implements ActionListener{ private String messages; @Override public void actionPerformed(ActionEvent e) { try { messages = outgoing.getText(); }catch (Exception ex){ ex.printStackTrace(); } outgoing.setText(""); outgoing.requestFocus(); } public String getMessages(){ return messages; } } public static void main (String[] args){ new UserInterfaceClient().go(); new Client().setUpConnection(); } } 

I understand that before clicking the button, the TestActionListener messages class field is null. As a result, it is this value that is passed through the method to another class. Why is that?

    1 answer 1

     public void setUpConnection (){ TestActionListener testActionListener = new TestActionListener(); ... } 

    This is the first actionListener.

     public void go(){ ... ActionListener actionListener = new TestActionListener(); ... } 

    This is the second actionListener. And after that you want them to be the same? In one class, you create an instance of the TestActionListener class, in another you pass an already created instance. And then everything will work.

    You can transmit:

    1. through class constructor
    2. through class method
    3. through the abstract class method. Defining all actions at the time of creation.
    4. Sign one of the classes for the events (similar to sendButton.addActionListener(actionListener) );
    • Thank you, if I correctly understood that if passed through a constructor, is it necessary to create a class instance variable? private TestActionListener actionListener; and the designer? public Client () {actionListener = new TestActionListener (); } and further through the getter to access the private field? I probably do not understand what the point, but I would like to understand. Following my logic, the field Private String messages of the TestActionListner method accepts a value after clicking a button. - wBb_m'Dz