Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an object created in another class

Tags:

java

object

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

like image 654
some_id Avatar asked Feb 25 '23 15:02

some_id


1 Answers

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;
}
}
like image 89
jmj Avatar answered Mar 07 '23 03:03

jmj