I have a infinite loop where I create an object. At the end of the loop, after creating the object I run it. When running the object the program will block because I have another infinite loop in my TcpClient-object. But sometimes this infinite loop will break because of an handled error, and will call a specific method in my mTcpClient-Object called "reconnect". There I would like to delete the mTcpClient object, so that the first infinite loop will go on and create a new mTcpClient-object.
How can I do this?
while(true) {
//we create a TCPClient object and
mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
@Override
public void reconnect() {
// HERE I want to delete this object
}
....
});
if (mTcpClient != null)
mTcpClient.run();
}
deletean object in Java. Can you clarify what you mean bydeletingthe object?TcpClient's garbage collection, you can overrideprotected void finalize()method ofObject.protected void finalize()is not good, and it does not promise that the object would be garbage collected. But to free any resources loaded through native methods, usingfinalize()is one way.