Returning array: reference or deep copy?

Discussion in 'Java' started by Ken, May 29, 2004.

  1. Ken

    Ken Guest

    If a class contains a private array, and a public method of that class
    returns that array using a return statement (e.g., return myArray),
    will the caller get a deep copy of the array or a reference to the
    encapsulated array? If it is the latter, is there a way to return the
    array as a constant so that the caller cannot change the values on the
    array?

    Thanks!

    Ken
    Ken, May 29, 2004
    #1
    1. Advertising

  2. Ken

    Ryan Stewart Guest

    "Ken" <> wrote in message
    news:...
    > If a class contains a private array, and a public method of that class
    > returns that array using a return statement (e.g., return myArray),
    > will the caller get a deep copy of the array or a reference to the
    > encapsulated array? If it is the latter, is there a way to return the
    > array as a constant so that the caller cannot change the values on the
    > array?
    >
    > Thanks!
    >

    Always references. If you want a copy (deep or shallow), you have to do it
    yourself. And there is no such thing as a constant return.
    Ryan Stewart, May 29, 2004
    #2
    1. Advertising

  3. Ryan Stewart wrote:

    >>If a class contains a private array, and a public method of that class
    >>returns that array using a return statement (e.g., return myArray),
    >>will the caller get a deep copy of the array or a reference to the
    >>encapsulated array? If it is the latter, is there a way to return the
    >>array as a constant so that the caller cannot change the values on the
    >>array?
    >>
    >>Thanks!
    >>

    >
    > Always references. If you want a copy (deep or shallow), you have to do it
    > yourself. And there is no such thing as a constant return.


    Not with arrays, anyway. If you use a List, you can do it via
    Collections.unmodifiableList().
    Michael Borgwardt, May 30, 2004
    #3
  4. Ryan Stewart wrote:

    > "Ken" <> wrote in message
    > news:...
    >
    >>If a class contains a private array, and a public method of that class
    >>returns that array using a return statement (e.g., return myArray),
    >>will the caller get a deep copy of the array or a reference to the
    >>encapsulated array? If it is the latter, is there a way to return the
    >>array as a constant so that the caller cannot change the values on the
    >>array?
    >>
    >>Thanks!
    >>

    >
    > Always references. If you want a copy (deep or shallow), you have to do it
    > yourself. And there is no such thing as a constant return.


    The array classes all expose public clone() methods, however, so if a
    shallow copy of the array is sufficient then

    return (ElementType[]) myArray.clone();

    Makes for a nice, simple, easy to read paradigm. If "ElementType" is a
    primitive type then there is no distinction between shallow and deep
    copying, and if it is immutable then performing a deep copy would be
    wasteful. In some other circumstances a shallow copy is what you would
    want anyway (you want the receiver to be able to mutate the returned
    array elements with effects visible to the host object).

    If you find that you really do want to make a deep copy of the array
    then you might want to consider whether your design can be improved; it
    is not necessarily so, but I wouldn't be surprised.


    John Bollinger
    John C. Bollinger, Jun 1, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    8
    Views:
    4,878
    Tris Orendorff
    Apr 29, 2004
  2. Alex
    Replies:
    2
    Views:
    1,206
  3. Replies:
    1
    Views:
    532
    Victor Bazarov
    Jun 24, 2005
  4. Replies:
    26
    Views:
    2,094
    Roland Pibinger
    Sep 1, 2006
  5. Replies:
    11
    Views:
    970
    Nindi
    Nov 14, 2006
Loading...

Share This Page