Access to an object created in another class

I am creating a thread in my main class. There is a timer in the stream that writes and reads on the socket.

I need to call a method in a thread class, for example writeSomething () from another class outside of where it was declared (Main).

How does an object refer to another class?

Edit

public static Thread connectionThread; ModelJTable table = new ModelJTable(); connectionThread = new Thread(new ConnectionThread(table), "connectionThread"); connectionThread.start(); 

I have a method in a thread class

 public void openFile(String fileName){ String request = "open;" + fileName; out.print(request); } 

I want to access if from another class (JTable class)

 String open = "open;" + getname + ";" + getpath; // This doesnt work ConnectionThread.openFile(open); 

This call causes an error.

There is no instance of the ConnectionThread type in Volume

+4
source share
2 answers

Either pass it in the constructor of the second class OR make it static in the first class, or serialize it

method 1: static

 Class A{ public static int a=0; } Class B{ public void someMethod(){ Aa = 10; } } 
+8
source

Pass the Thread reference to the class that should call the method.

0
source

All Articles