开发者

Accessing an object created in another class

开发者 https://www.devze.com 2023-01-30 07:11 出处:网络
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

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 Connec开发者_如何学PythontionThread(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;
}
}


Pass a reference to the Thread to the class that needs to call the method.

0

精彩评论

暂无评论...
验证码 换一张
取 消