This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes How do you copy an object or is it done automagically? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How do you copy an object or is it done automagically?" Watch "How do you copy an object or is it done automagically?" New topic
Author

How do you copy an object or is it done automagically?

joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
I'm creating a socket called client that I pass to a list for later use. As shown in the code below I re declare client as Socket. What happens; does the list I sent it to contain a reference to nothing or a copy of the object? For some reason this doesn't seem to be the way to store a client socket.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Not clear to me what your problem is.

Surely you understand that your line 3 assigns a reference to a Socket object to the local variable "client"

It seems likely that your object with the reference name "conListener" keeps a reference to that Socket after the local variable "client" goes out of scope by line6.

So there is no "copy of the object" involved, possibly copies of references to an object.

Bill
joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
William Brogden wrote:Not clear to me what your problem is.

Surely you understand that your line 3 assigns a reference to a Socket object to the local variable "client"

It seems likely that your object with the reference name "conListener" keeps a reference to that Socket after the local variable "client" goes out of scope by line6.

So there is no "copy of the object" involved, possibly copies of references to an object.

Bill



I understand that it is a reference, but when the reference goes out of scope is that OBJECT of client still safe to work with to which I have a stored copy of the reference on a list or does the object disappear when the reference goes out of scope? I put it (the reference to the client socket) on a list and use it create outputstreams and inputstreams later on. I also create multiples of the client socket (more users logging in) so will that object go away when a new person joins the server? I'm using it the way I have it coded, but I'm not sure if my reference is looking at unsafe memory that is no longer assigned to anything (ie the object will eventually be GC'd.)

(ITS THE C programmer in me I think getting lost here.)
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
joe vasher wrote:I understand that it is a reference, but when the reference goes out of scope is that OBJECT of client still safe to work with to which I have a stored copy of the reference on a list or does the object disappear when the reference goes out of scope? I put it (the reference to the client socket) on a list ...

Objects are only eligible for garbage collection when there are no (reachable) references to them. If you've put a reference to an object in a List then that object cannot be eligible for garbage collection (unless there is no reference to the List).
Kartik Mavani
Greenhorn

Joined: May 21, 2013
Posts: 5
joe vasher wrote:I understand that it is a reference, but when the reference goes out of scope is that OBJECT of client still safe to work with to which I have a stored copy of the reference on a list or does the object disappear when the reference goes out of scope? .


Here you do not store a copy of the object. You are storing the "Reference" to the client object. When the object goes out of scope it becomes eligible for garbage collection. So the reference that you have stored will be pointing to no object. So its not working.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kartik Mavani wrote:
joe vasher wrote:I understand that it is a reference, but when the reference goes out of scope is that OBJECT of client still safe to work with to which I have a stored copy of the reference on a list or does the object disappear when the reference goes out of scope? .


Here you do not store a copy of the object. You are storing the "Reference" to the client object.


He already told us--and you quoted it--that he understands that.

When the object goes out of scope it becomes eligible for garbage collection. So the reference that you have stored will be pointing to no object. So its not working.


No, that's not correct.

First, objects don't have scope. Variables do.

Second, as already explained right above your post, the List has a reference to the object, so the object is NOT eligible for GC as long as the List is reachable.

Please do read the rest of the thread before posting. Thanks.
 
 
subject: How do you copy an object or is it done automagically?
 
Similar Threads
System.out.println()
resolve method java.nio.Path's method
Object serialization with Java NIO
Create a class which can manage a collection of objects
JPanel content refresh is not happenning