Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
joe vasher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic