3 Replies - 717 Views - Last Post: 17 April 2011 - 06:35 PM Rate Topic: -----

#1 Lowkey1979  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-April 11

Array of objects, same object copied into every element of array?

Posted 17 April 2011 - 04:59 AM

Hi there,

I'm really not sure why this isn't working. I declare an array, initialise each object in the array, specifying the index of the element, and then output the results. Instead, every element in the array has the same object. Here's the code, pruned back as far as possible. These first five objects are defaults created for testing, and some method calls to show you what I mean.
Request requestArray[] = new Request[10];
requestArray[0] = new Request("005", "3344556", "COSC2135", "WebLearn",
                "Work Commitment", 7, "Denied",
                MainMenu.dateFromString("13/09/2011"));
        //next line for testing, remove
        requestArray[0].oneLineDisplay();
        //for element [1], need to use default constructor.
        requestArray[1] = new Request();
        requestArray[1].setAssessmentName("Assignment1");
        requestArray[1].setCourseCode("COSC1077");
        requestArray[1].setRequestID("1006");
        requestArray[1].setReasonGiven("Flu");
        //next line for testing, remove
        requestArray[0].oneLineDisplay();
        requestArray[2] = new Request("21450", "2345678F", "ISYS1117",
                "Assignment2", "Husband Sick", 3, "Granted",
                MainMenu.dateFromString("25/04/2011"));
       
        requestArray[3] = new Request("089", "9987564X","COSC1077", "Weblearn",
                "In Hospital", 7, "Pending",
                MainMenu.dateFromString("24/05/2011"));
       
        requestArray[4] = new Request("1245", "3145091", "ISYS1117",
                "Assignment3", "Carer’s leave", 5, "Granted",
                MainMenu.dateFromString("02/04/2011"));
        //next line for testing, remove.
        requestArray[0].oneLineDisplay();



Despite each object being assigned to different elements, the output of the three oneLineDisplay() calls is as follows:

005 COSC2135 WebLearn 13/9/2011 Denied
1006 COSC1077 Assignment1 1/1/1900 None Entered
1245 ISYS1117 Assignment3 2/4/2011 Granted

All of those are the result of calls to output the same object, at element [0].

First line is correct. But the second call to requestArray[0], it outputs the values of requestArray[1], and the third call has the value of requestArray[4]. So in essence, after each new object has been created with the correct parameters, that object is copied over all other objects already in the Array!

So, um yeah. I dunno, what do you think is going on here??? I've looked everywhere for the answer, no success. New to Java.

Is This A Good Question/Topic? 0
  • +

Replies To: Array of objects, same object copied into every element of array?

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,001
  • Joined: 20-September 08

Re: Array of objects, same object copied into every element of array?

Posted 17 April 2011 - 05:24 AM

Make sure the fields of the Request class aren't declared static. If they are, you need to read up on what static means

This post has been edited by g00se: 17 April 2011 - 05:24 AM

Was This Post Helpful? 3
  • +
  • -

#3 Lowkey1979  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-April 11

Re: Array of objects, same object copied into every element of array?

Posted 17 April 2011 - 03:18 PM

View Postg00se, on 17 April 2011 - 05:24 AM, said:

Make sure the fields of the Request class aren't declared static. If they are, you need to read up on what static means


Thanks! That was it,
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Array of objects, same object copied into every element of array?

Posted 17 April 2011 - 06:35 PM

View Postg00se, on 17 April 2011 - 07:24 AM, said:

Make sure the fields of the Request class aren't declared static. If they are, you need to read up on what static means

g00se you spotted that right away !!! Jolly good show
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1