• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I add a object (copy) to a que or hashmap, and not a reference?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm writing a server & client app that will except multiple clients (For learning sakes.) I have not grasped the concepts of a copy of a OBJECT or a ref to a packet.

If I write a method in my server such as....



To be sure in my program I actually wrote a Constructor that copies the contents of one object to the new object.. And I think someday when I'm smarter, I can use the "opperand=" method in a class to copy, but not sure how that works Or I could be confusing that with c++..<SMILE

Not sure If I need to go that extra step.. Also I read a article on clone object.. (which adds a whole new level of confusion but seems to be way of handling it.)

Thanks in advance..
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java doesn't have operator overloading, so changing what = does is not going to happen. It always copies the value; for references, the value is not the object itself but a reference to the object.

The best choice is to use a copy constructor:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One important thing to understand is that in Java, variables are references to objects. A variable is not the object itself - it is a reference to the object.

With this in mind, let's look what this code does:

Line 1: Declares a variable copyMO, creates a new MyOBJ object and makes copyMO refer to that new object. NOTE: Creating the new object is unnecessary, since in the next line you're making copyMO refer to another object, throwing away the new object that you're creating here.

Line 2: Make copyMO refer to the same object as that mo refers to. NOTE: This does not copy the object that mo refers to.

Line 3: Add the object to the queue. This actually adds a reference to the object to the queue.

Line 4: Set copyMO to null.

The effect of these four lines of code is essentially the same as simply doing:

Declaring a new variable, assigning it to the same object as mo, and then adding it to the queue through the new variable is unnecessarily complicated and doesn't do anything to create a copy of the actual object. If you really want to copy the object, then Rob's suggestion to create copy constructor is good. You would use it like this:

Note: Setting copyMO to null is not necessary.
 
joe vasher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quick explanation of where the items are coming from to help explain my confusion. I have packet objects coming in over a socket stream I want to put them in the que to be handled by the server and dispatched back out to the clients. So the object is created by the ois.readObject() and if i add this to my que wont my que point to the same memory if more then 1 object is added? (thus the reason I need to make more copies.)

Thanks for the greate explanations above.
 
Ranch Hand
Posts: 63
Spring Java Google App Engine
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use clone() method to make more copies of an object
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

joe vasher wrote:So the object is created by the ois.readObject() and if i add this to my que wont my que point to the same memory if more then 1 object is added? (thus the reason I need to make more copies.)


In this case, it is not necessary to copy the objects. ois.readObject() will create a new object each time you read an object; no need to copy that object. Just put it in the queue.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:no need to copy that object. Just put it in the queue.


Put a reference to it in the queue.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic