It's not a secret anymore!
The moose likes Beginning Java and the fly likes Object Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object Arrays" Watch "Object Arrays" New topic

Object Arrays

Todd Smith Todd

Joined: Dec 11, 2002
Posts: 4
I need help I am working on a program for class. I need to Pass data in a object array from my Main Driver to class in a package but I can get my code to work. I need all the help I can get. From some reason when I loop back thru the array I alwasy get to last thing I load in to the array in any position I look at it the array if that make sence.

Here is the test data

123Smith, Jimmy C 8.012.0 0.0 2.0
166Render, MeleneC10.015.0 0.0 0.0
166Render, MeleneS 0.0 0.0 5.0 0.0
184Towner, John C 0.0 0.0 9.7 7.2
184Towner, John S 4.6 7.8 1.5 1.9
234Hope, Susan S 0.0 0.0 2.0 0.0
278Jones, Tom W 0.0 0.0 8.9 0.0
341Wilson, ClaudeS 0.0 0.0 0.0 5.0
345Higins, Bill C 1.0 1.0 1.0 1.0
345Higins, Bill S 2.0 2.0 2.0 2.0
456Bean, Ryan S 0.0 3.0 4.0 0.0
479Kringle, ChrisS 0.0 0.0 5.0 0.0
479Kringle, ChirsW 0.0 1.3 0.0 0.0
501George, Paul S14.9 7.4 8.7 9.4
501George, Paul W 0.0 0.0 0.0 4.5

[ EJFH: Edited to add CODE tags for formatting ]
[ November 30, 2004: Message edited by: Ernest Friedman-Hill ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

This is happening because you made all the member variables in the class BillRecord static. That means that all the objects of that class share the same copies of these variables, so no matter how many you construct, they'll all always have the same set of values: the ones assigned to the last one constructed.

To fix, just make all the members of BillRecord non-static.

Note that static variables are the exception, rather than the norm. Furthermore, functions shouldn't commnicate through static variables as if they were global variables in some other language. In general, make members non-static whenever possible, and also use local variables defined in a method in preference to member variables defined in a class whenever you can.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Object Arrays
It's not a secret anymore!