aspose file tools*
The moose likes Beginning Java and the fly likes Accessing instance vars from objects in an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing instance vars from objects in an ArrayList" Watch "Accessing instance vars from objects in an ArrayList" New topic
Author

Accessing instance vars from objects in an ArrayList

Paul Taylor-Green
Greenhorn

Joined: May 20, 2010
Posts: 5
Hi... wondering if anyone could help me.

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.


Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
The problem is not with this code, it's with MyPlayerTest :-) Carefully check the signature of the name fields. (Did you happen to use Eclipse Ctrl+1 quick assist to create those fields in MyPlayerTest, by any chance? For me, doing so resulted in code that exhibited the behaviour you've described.)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
Welcome to the Ranch

Can't see what is going wrong in that code; please provide more details of the class you are putting the names into.
Are you sure those are instance fields; the behaviour looks a bit like what you expect if you declared the fields as static by mistake.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

What does your class MyPlayerTest look like?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Paul Taylor-Green
Greenhorn

Joined: May 20, 2010
Posts: 5
Thanks for your responses. Below is the MyPlayerTest code. To edit, I'm just using gedit.

Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
Paul Taylor-Green wrote:
static String firstName;
static String surname;
[/code]


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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
Istvan Kovacs wrote: . . .
Your fields are static . . .
I think all three of us guessed that
Paul Taylor-Green
Greenhorn

Joined: May 20, 2010
Posts: 5
Thank you _so_ very much - all of you! I'd been scratching my head for days! Much obliged to you all.
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
You may want to try an IDE. Eclipse, for example, can give you warnings such as when you access a static (class) member through an instance - a potential programming error, as your case demonstrates. You can also integrate it with other checkers, such as FindBugs, to help you eliminate common problems more easily.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
Depends how long yo have been programming. Most of us think you ought to avoid IDEs until you have several months' experience at least.
Paul Taylor-Green
Greenhorn

Joined: May 20, 2010
Posts: 5
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!
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
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.

http://charlespetzold.com/etc/DoesVisualStudioRotTheMind.html
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This kind of "top down" programming is called "programming by intention" in the agile community; it's a good thing. Yet another variant is the "Program Design Language" (PDL) approach, where you write a routine in English as a series of comments, and then below each comment, add the code to implement that step.


[Jess in Action][AskingGoodQuestions]
Paul Taylor-Green
Greenhorn

Joined: May 20, 2010
Posts: 5
OK, I'm warming to an IDE! Trouble is, with NetBeans for example (although I like the hints that NetBeans comes up with.), I get lots of "symbol not found" messages in the margin of the code, yet if I compile those same classes at the command line, even with -Xlint, I get no error messages and they run just fine. What you said about the different styles of approaching programming projects is interesting, and definitely something I will have to look at. Thanks again for the tips.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing instance vars from objects in an ArrayList