I create a thread in my main class. The thread has a timer which writes and reads on a socket.
I need to call a method in the thread class e.g writeSomething() from another class outside of where it was declared(Main).
How is the object referenced from 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 the thread class
public void openFile(String fileName){
String request = "open;" + fileName;
out.print(request);
}
I want to access if from another class(the JTable class)
String open = "open;" + getname + ";" + getpath;
// This doesnt work
ConnectionThread.openFile(open);
This call gives an error
No enclosing instance of the type ConnectionThread is accessible in scope
Either pass it in constructor of second class OR make it static in first class, OR serialize it
way 1: static one
Class A{
public static int a=0;
}
Class B{
public void someMethod(){
A.a = 10;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With