![]() |
|
|
|||||||
![]() |
Java - Returning array: reference or deep copy? |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
"Ken" <> wrote in message
news: m... > 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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
Ryan Stewart wrote:
> "Ken" <> wrote in message > news: m... > >>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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to burn copy CD/DVD and create Data/Audio/Video Disc? | dvdloveri | Software | 6 | 07-27-2009 07:58 AM |
| DVD Copy for Mac OS Users | reallyone | Software | 2 | 03-22-2008 01:38 AM |
| Copy DVD Gold - a new software that can copy dvd | heren | DVD Video | 4 | 10-12-2005 06:26 AM |
| Getting Around Copy Controls | Ablang | DVD Video | 0 | 04-01-2005 06:09 AM |
| Dvd X copy express 3.0.1 | Alexandre Magny | DVD Video | 2 | 09-12-2003 03:02 PM |