• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Brick House Java Bean Question

 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The (unexpected) output is:

Here's the code, which can speak for itself, I think.


I think I see that because "this.chest = chest;" is really "this.chest = new String(chest);", it means that my reference that's in the array is pointing to an old copy. Fine. Weird, but fine. Now, how does one deal with it? I thought of:



But that's not very nice now, is it?

This is one of those "there must be a better way" things (without removing the key concept here: the array of attributes). They all might not be String objects, by the way.

--Dale--
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dale Seng:

But that's not very nice now, is it?



I'm not sure why you think this is "not nice"; there are two ways to fix your code, and this is one of the two. It's actually the less preferable one, but I could live with it if you had special requirements.

The other one is to get rid of the arrays and loops and lists of names and such drek, and just have



The version with the arrays and loops is just plain nasty: hard to understand and hard to change. An awful lot of mess and overhead to introduce just to (presumably) avoid a bit of work in updating the "toString()" method if attributes are added.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actaully, that way is just fine. That's the point of encapsulation -- it deosn't matter *how* it is implemented, the exposed methods (the getters and setters) will work. The inner workings is just a black box.
 
Dale Seng
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gentlemen: Thank you.

The reason I said it was "not nice" was because Eclipse wouldn't write it for me ;-)

Getting rid of the attributes array isn't really an option because I've got bunches and bunches of attributes that I need to get via EL, so they need a "named getter".

But I wanted to slam data into this, and other beans that extend the same abstract class, by using something along the lines of "attributes[i] = resultSetItem.getObject(i)".

It works the way I have it; I guess it's nice enough.

Thanks again.

--Dale--
 
reply
    Bookmark Topic Watch Topic
  • New Topic