Hi,
I am in a situation where I need a language feature, which I am not
sure exists. I have two classes, one (let's say A) contains a list of
data and the other (say B) contains the functionality to display that
list. Now I want to have private data inside A, which should not be
accessed by user code. Problem is, that I want B to have access to this
data as well.
The only 'solution' I can come up with, is to make that data public
and document that it must not be modified. Does not feel like clean code
tho. I am also trying to avoid sophisticated work-arounds, since the
code will run on devices with very limited memory and cpu.
Thanks for your help!

Signature
jb
(reply address in rot13, unscramble first)
Bjorn Borud - 24 Jun 2005 14:34 GMT
["Jakob Bieling" <argfhesNGtzkQBGarg@rot13.com>]
| The only 'solution' I can come up with, is to make that data public
| and document that it must not be modified. Does not feel like clean code
| tho. I am also trying to avoid sophisticated work-arounds, since the
| code will run on devices with very limited memory and cpu.
you can put the classes in the same package and use package-private
access, but what you *really* want to do is to think long and hard
about your design; if you need to expose the inner workings of one
class to another class, chances are that your code will be very hard
to debug, understand and maintain.
bottom line: _I do not recommend you do this_!
at the very least I would define a getter which returns a reference to
the data in question, using the most general data type you can live
with, so that access to the internals are defined, and if possible,
somewhat restricted. you could also ensure read-only access or make a
defensive copy of the data, but from what I understand, your
environment is memory-constrained so the defensive copy might defeat
the purpose.
for example if you have an instance of a HashMap inside one class that
you wish to return you could for instance do:
public Map getFoo() {
return Collections.unmodifiableMap(myHashMapInstance);
}
which would give you (reasonably) safe access to the underlying data
without making too many commitments.
-Bjørn
Bjorn Borud - 24 Jun 2005 15:12 GMT
[Bjorn Borud <borud-news@borud.no>]
| public Map getFoo() {
| return Collections.unmodifiableMap(myHashMapInstance);
| }
actually, since you said "list" I would assume that the following
might be more along what you need:
public List getFoo() {
return Collections.unmodifiableList(myList);
}
-Bjørn
Roedy Green - 26 Jun 2005 09:04 GMT
>at the very least I would define a getter which returns a reference to
>the data in question, using the most general data type you can live
[quoted text clipped - 3 lines]
>environment is memory-constrained so the defensive copy might defeat
>the purpose.
These getters and setters can be optimised out of existence by a
compiler like JET. They give you the flexibility to rearrange the data
structure of A without impacting B.

Signature
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
Tim Miller - 24 Jun 2005 15:15 GMT
> Hi,
>
[quoted text clipped - 10 lines]
>
> Thanks for your help!
You can use a method to access the data (which is good programming
practise anyway), but make the return value a copy of the list in
the method before it is returned. This will stop any user code
changing which elements are in the list, but will take up more memory
John Currier - 25 Jun 2005 22:04 GMT
That's a situation where I long for C++'s const keyword...you'd return
the collection of objects that can't be modified without explicitly
breaking rules.
John
Tor Iver Wilhelmsen - 26 Jun 2005 07:59 GMT
> That's a situation where I long for C++'s const keyword...you'd return
> the collection of objects that can't be modified without explicitly
> breaking rules.
Check out java.util.Collections.unmodifiable*();
John Currier - 29 Jun 2005 04:26 GMT
The unmodifiable* versions of the various collections do not protect
the contents of the collection, only the collection itself.
John
http://schemaspy.sourceforge.net
Jakob Bieling - 24 Jun 2005 15:48 GMT
Hi,
thanks for the quick responses. I rethought my design and did find
the cause. The data in A that B needed to access, should have been in B
alltogether (because it partly resembled the displaying state of the
data). It also solved the other implementation problems that came along
with that ;)
Thanks again :)

Signature
jb
(reply address in rot13, unscramble first)
Roedy Green - 26 Jun 2005 09:02 GMT
> Problem is, that I want B to have access to this
>data as well.
You can make the data protected, then do B extends A.
You might make B an inner class of A. You can create a small package
and put both A and B in it and make the data default.

Signature
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes