progNewbie
progNewbie

Reputation: 4812

Java: Delete and recreate object

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();
}

Upvotes: 1

Views: 1283

Answers (1)

pepers
pepers

Reputation: 315

mTcpClient = null; Setting an object to null will cause the garbage collector to delete the object for you.

Upvotes: 1

Related Questions