How to properly copy and object

I have an object that I want to keep immutable, I want to provide a copy() method so that it returns a new instance of the Object its copying. But I wanted to know, just for clarity sake, if I did this:
public MyClass copy() {
  MyClass copy = new MyClass();
  copy.byteProperty = this.byteProperty;
  return copy;
}And please note that byteProperty in this case is a byte[].
Is the reference to the copy's byteProperty array a copy of the originals or a reference to it?
I would never ever want the user of my class to be able to change the byteProperty of both the class they copied and the new copy by accident.
For example, would doing this:
MyClass orig = new MyClass(someByteArray);
MyClass copy = orig.copy();
copy.methodToChangeInternals(); //this could internally change around the byte[]would the call to methodToChangeInternals() change the byte[] in both instances?
Thanks!

Encephalopathic wrote:
better than asking this question: test it out for yourself. You get the answer in several ways, by reading and by doing. There's no doubt in my mind that by doing this, you learn more and have a deeper understanding.You are right, and so I did...
I learnt what flounder mentioned, so I implemented by copy() method differently. Using the ArrayUtils.clone() method, I made a clone of the internal state, whic worked fine.
I then read ejp's post, and changed my class to implement Cloneable... and changed the method to clone()
Thanks for the help guys! Very fast turn around :)

Similar Messages

  • How to do copy and paste of a subtree in a JTree?Thank you

    How to do copy and paste of a subtree in a JTree?Any examples?Thank you!

    you need to do a 'deep-clone' of your subtree and add it to your destination node. eg:
        public Object clone() {
            DefaultMutableTreeNode theClone = (DefaultMutableTreeNode )super.clone();
            for (int i = 0; i < children.size(); i++) {
                DefaultMutableTreeNode child = (DefaultMutableTreeNode )children.get(i);
                theClone.add((DefaultMutableTreeNode )child.clone());
            return theClone;
        }thomas

  • How can I copy and paste an entire page on the new Pages?

    How can I copy and paste an entire page on the new Pages?

    Open one document in Pages v5.5.3, Open a second document in Pages v5.5.3. Select, copy, and paste one page between source and destination documents. Document objects such as Text boxes, shapes, images, tables, etc must be individually selected, copied, and pasted.

  • How do I copy and paste from one page or file to another page or file - on the same screen?

    How do I copy and paste from one page or file to another - on the same screen?

      Open both of your images in the Editor. Click on the image you want to copy and press Ctrtl+A (select all) followed by Ctrl+C (copy) then click the tab for your other image and press Ctrl+V (paste)
    Alternatively click on your background image then simply drag the second image from the photo/project bin and drop it into the main editor workspace on top of the background image. Use the move tool to position and the corner handles of the bounding box to scale.

  • How do i copy and print groups from my address book?

    How do I copy and print groups(with contact information) from address book? I want to copy a group of contacts in list form that ideally I can paste into Excel.

    I'm not sure exactly what you are asking.  If you are coming from pre-Mountain Lion, then Address Book has changed to Contacts.
    Open Contacts.
    Select an Account in the group list, if you want the contact in a specific group. If Groups aren't showing, you can display them from the View menu.
    Click the Add card button ( + ).
    Enter the information for the contact.
    If you need multiple entries for a particular field type, a new blank entry will show up below the one you are adding.
    If you need other fields, use the Card menu to Add a new field type.
    If you want to change the default fields available when you create a new card, choose Edit Template…
    If you have sent emails to someone and you want to get that address to add to a Contact, use the Previous Recipients command in the View menu in Mail.
    If you need to add a new Group, hover the cursor over the account name, and a + button will appear. Click it.

  • The edit button disappears when I have the link window open, so how do I copy and paste address for email link?

    When I want to create a link to a web page in an email, I click the "link" icon and a window appears asking for the address. But simultaneously, the "edit" function and all other functions disappear from the Firefox toolbar. With no edit, how can I copy and paste the address? I have tried highlighting the address and dragging it into the window, but it won't drop into the window.
    I have resorted to copying the address to a document and then pasting it into the link window, but there must be a less cumbersome way.

    You see the orange (on Linux gray) Firefox button if the Menu Bar is hidden.<br />
    You can make the Menu bar visible via View > Toolbars, also accessible via Firefox > Options.<br />
    If you need to access the hidden Menu bar then press F10 or hold down the Alt key to make the Menu Bar appear temporarily.<br />
    * View > Toolbars : [X] Menu Bar

  • How can I copy and paste a caption for several pictures that are all the same?

    I'm trying to understand the process of importing, labeling and organizing pictures I'm importing from my iPhone into iPhoto 11.
    So far I am importing only pictures that relate to one another; ie taken at the same event.  I have 18 pictures from one hike.  I'd like to label each of those pictures the same; ie "hike at Bear Creak 2/15."  Can I copy and paste the same label to all 18 pictures?  How would I copy and paste?
    There are lots of You Tube videos on the subject of iPhoto and that's how I learn best.  If you know of a really basic one to watch I'd appreciate it. 
    I think I have to take a class at the Apple Store on this basic organizing because it's really escaped me in the 9 months I've had this Mac (switched from PC.)
    Thanks,
    Ginny

      I have 18 pictures from one hike.  I'd like to label each of those pictures the same; ie "hike at Bear Creak 2/15."  Can I copy and paste the same label to all 18 pictures?  How would I copy and paste?
    Ginny,
    select all 18 photos at once in iPhoto on your Mac and then use the menu entry "Photos > Batch Change"
    That will change the titles or descriptions of all photos at once.
    -- Léonie

  • HT1338 How do I copy and paste a flyer from pages to the main body of the e-mail

    How do I copy and paste a flyer that was designed in pages to the main body of the e-mail

    Print or Export to pdf and drag that into your eMail.
    Peter

  • How can I copy and paste a pdf file from my picture file and paste onto a document?

    How can i copy and paste a pdf file onto a document.  I am trying to place and article onto my
    Reverbnation Press page.  They only give you the option to either place a url link or paste a document.  There
    is no uploading for the Press page.  thanks, elena

    The Digital Editions forum is here, in case this is what you are talking about:
    http://forums.adobe.com/community/adobe_digital_editions
    If you are not, I apologize for the misunderstanding.

  • How do I copy and paste text from a converted Word document?

    I just converted a pdf file into word.  I want to copy and paste the informatiuon into another document.  How do I do that?

    Hi Sara.  Tanks for responding.  If I cannot figure this out (I  was up
    until 2am last night), I want to cancel my subscription.
    Here are two files you can check on/help me with.  When I try again,  it
    comes up 'an error has occurred when trying to access the service.'
    Please advise.
    Pat Tomassi
    In a message dated 11/1/2014 12:55:23 A.M. Eastern Daylight Time, 
    [email protected] writes:
    How  do I copy and paste text from a converted Word document?
    created by Sara.Forsberg (https://forums.adobe.com/people/Sara.Forsberg) 
    in Adobe Acrobat.com Services - View the full  discussion
    (https://forums.adobe.com/message/6888908#6888908)

  • How do I copy and paste an address list from an Excel file to an email on my iPad2?

    I am using Dropbox to maintain email and text lists. How do I copy and paste these lists into the email "to" box on my iPad2 and iPhone?

    I do not fully understand your question. However, you can use the GoodReader app to download and excel file from DP and open it is GoodReader. Then copy the address abd paste it in the email.

  • How do I copy and paste files from a windows remote server to my mac?

    How can i copy and paste files from a remote server (windows) to my mac? i have installed cord to connect to the server but now i can´t cpy the files to my mac!

    Hi Sara.  Tanks for responding.  If I cannot figure this out (I  was up
    until 2am last night), I want to cancel my subscription.
    Here are two files you can check on/help me with.  When I try again,  it
    comes up 'an error has occurred when trying to access the service.'
    Please advise.
    Pat Tomassi
    In a message dated 11/1/2014 12:55:23 A.M. Eastern Daylight Time, 
    [email protected] writes:
    How  do I copy and paste text from a converted Word document?
    created by Sara.Forsberg (https://forums.adobe.com/people/Sara.Forsberg) 
    in Adobe Acrobat.com Services - View the full  discussion
    (https://forums.adobe.com/message/6888908#6888908)

  • How do I copy and paste an image form the internet to Keynote. I have done this successfully in the past by selecting copy and then paste by using the right click function and now am unable to do so. I am thinking it is an outdate OS problem...not sure.

    How do I copy and paste an image from the internet to Keynote. I have done so successfully in the paste by using the copy and paste right click functions but am unable to do so now. That was about a year ago that I attempted that I was able to do this. Thank you for your help.

    Some images are copy-protected.
    If all else fails, you can make a screen shot of the image: 
    With the image visible in your browser, press CMD-Shift-4 and your cursor will change to crosshairs.
    Position the cross hairs at one of the corners of what you want to copy and drag to the opposite corner, then release the mouse. An copy of the image will be saved to your desktop.
    You can now edit and use this image.
    Be sure to respect copyrights.
    Message was edited by: bwfromspring hill

  • How can I copy and paste text into Word in Acrobat 9 Pro

    How can I copy and paste text into Word from Acrobat 9 Pro?

    You might find it easier to export the file to Word under File > Export > Word in Acrobat 9.

  • How can i copy and referencing the value of a sub-total of a category into another cell?

    How can i copy and referencing the value of a sub-total of a category into another cell?

    Cells in Category  rows 'exist' only when those categories are inserted using the Reorganize table. Those cells cannot be referenced from other cells.
    If you want a category summed, you will need to do that in a cell that is still visible when the table is not sorted into categorie using the Reorganize pane. The function to use is SUMIF. You can find information regarding SUMIF (and SUMIFS) in the iWork Formulas and Functions User Gude. The guide may be downloaded using the link in the Help menu in Numbers '09.
    Regards,
    Barry

Maybe you are looking for

  • Can not retrieve data in Crystal Report

    Hi, I have a problem to load data from <i><b>Crystal Report</b></i> in an SBO form with the ActiveX DI object : the Crystal Report Viewer message display : <b><i>'Failed to retrieve data from the database. Details [Database Vendor Code:208]'</i></b>

  • Character sets and ado

    I have a table with a clob field on an Oracle 8.1.7.4 database. When querying the clob field via odbc and ado the value is truncated. The Oracle server and client are using a WE8ISO8859P1 character set. Has anyone come across this before. Thanks.

  • Opportunity Team - Owner

    Hi Folks, Using the Opportunity Team functionality: When a user creates an Opportunity against an account that they do not own, the account owner is added to the 'Opportunity Team' along with the user who created the opportunity in the first place. I

  • PO creation error - Info-record

    Hi All, I am trying to create the PO for a particular material,but i am getting an error on saving the PO. "Please contact the responsible person for Info-rec" The Info-record for that material for that vendor is present when i checked in the t-code

  • I cannot find the new data in the database

    Hi I'm newbie at SQL. I read and watched a lot about the SQL. But still can't figure out the problem. THE PROBLEM: we are using accounting software for our business. the software keeping records of clients names and other information. when I open the