Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

encapsulating of array

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I don't know how can I encapsulate array member in class.

It seems like the method setTeamAt is unneedful.
The method getTeams() is getter(it's right), but it can work like setter too(it's wrong).
It's possible

I would disallow it, I don't know any trick to do it.


Is there any "nice" trick to do it?

Thanks.
 
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
A fundamental rule of good Java programming is "don't return a reference to an array member unless you don't care what changes are made to it." Since it's rarely the case that you truly don't care, this rule can't be shortened to "Don't give out references to array members."

You could return a clone of the array,



or you could provide a read-only interface:



Either of these solutions would let clients get access to the JTextFields but not modify the array.
 
dar
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, Ernest.
[ June 24, 2004: Message edited by: dajac chison ]
reply
    Bookmark Topic Watch Topic
  • New Topic