Re: Design question
From: dar7yl (no_reply_at_accepted.org)
Date: Sat, 26 Feb 2005 03:27:10 GMT
"jonck" <email@example.com> wrote in message
> I have been struggling with this for days now, but it does not look
> like I'm going to figure it out. Here is the situation:
> I have a class that stores values that it read in from a database, lets
> call this class Storage. I display this data using a JPanel (which I
> will call DisplayPanel) with several JTextFields, JComboBoxes, JPanel's
> with pictures drawn on them, etc... So in other words, when the user
> selects a new record, the DisplayPanel loads in a new Storage class,
> and calls all the getters of Storage, something like this:
> Now this is all fine, but now comes the time when the user has made
> some changes and I want to store those back into the Storage class.
> I now have to do the same thing again, but this time in reverse:
> Now there are about 20 fields per Storage class and I have about 10
> different types of Storage classes, so it seems kind of wasteful, very
> non-OOP and error-prone to be doing it as I described above. It seems
> like a pointer would be really useful here, so that I could just pass
> the pointer as the model value to the view, and then when the view is
> updated, the view can update the model, of which it has the pointer.
> But since Java is a pass-by-value language this won't work for me.
> Does anyone have any suggestions what a better way of doing things
> might be in the above described situation?
> Thanks, Jonck
Whatever gave you the idea that Java is pass-by-value?
As far back as I can remember, it has been pass-by-reference
for all object parameters.
You can even consider all object variables to be pointers.
(although you can't perform pointer arithmetic like C/C++)
Object obj = new Object();
otherobj.method(obj); // obj is not copied, but passed by reference
so you could try:
class DisplayPanel extends Panel
// ... more accessors
void storeBack( Storage store )
store.setFirstName( getFirstName() );