The following code demonstrates the nub of the problem that I have, that is how to access object methods and instance variables stored in an ArrayList. Whether I have one or more lists, when I try to access them all of the entries appear to store the last-added details.
What am I doing wrong (in this code example) to have all of the array elements equal to Karen Lewis? Am I trying to recall them incorrectly? Am I storing them wrong? Should I be using something other than an ArrayList?
Any tips gratefully received. Thanks.
Paul Taylor-Green wrote:
static String firstName;
static String surname;
Your fields are static - shared by all instances of the class (as they belong to the class itself, not a specific instance), and you're overwriting their values whenever you create a new instance and invoke the constructor. Remove static and you'll be OK.
Paul Taylor-Green wrote:I did have a look at NetBeans and I had Eclipse on an old computer, but I thought that they were both way too confusing. I might have to re-visit them given what you've suggested; the ability to easily catch what, here, was a fundamental mistake that I was unable to "see.". I do really like gedit, though!
Campbell Ritchie is right in that an IDE (with its code completion features advising you about the available fields and methods) can lead to bottom-up design (you first design and write the lower level class so you can use it in your upper level class, which will probably mean you design it before you actually have an idea on how you want to use it). However, they can also be used the other way around: code your top-level class, pretending you have the lower-level details - this will give you lots of errors (no such type, no such method...); then you can use the IDE's quick-fix features to create the skeletons of types and methods, so you 'only' have to deal with the real code that goes into them. I find this a great time saver.