Copy a socket over to another socket, is that same socket still active? java

communication, connection, java, sockets, variables

Solution

Since there is still a reference to the object that was originally `sock` (in the form of `sock2`), the Java garbage collection will not dispose of it, and you can still use `sock2` as if it were `sock`.

Problem

If I have a socket variable named 'sock' and lets say down the line for some reason you have to create a new socket, lets say its called 'sock2', and you set 'sock2' to equal 'sock'. And then you throw away the variable 'sock'. Will the connection still work, will 'sock2' still be able to communicate with the other end of the socket that was originally in 'sock'?

Original source