This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Populating an array of objects, bug? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Populating an array of objects, bug?" Watch "Populating an array of objects, bug?" New topic
Author

Populating an array of objects, bug?

eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
I have a bug in the below, that causes all of the array slots to be populated with the same value.

I have successfully populated arrays of objects in the past using this same technique. But now I can not get this to work.

if I populate array[0] the value shows up in array[1] and array[2] as well.


Code:

public class TestObj
{
public static int i1;
public static String s1;

//----------------------------------------------------
public static TestObj populateObj(int iIn)
{
TestObj testObj1 = new TestObj();
testObj1.i1 = iIn;
testObj1.s1 = String.format("The Number is %2d", iIn);
return testObj1;
}//end method
//----------------------------------------------------

public static TestObj[] buildObjArray()
{
TestObj[] myTestObjArray = new TestObj[3];
String s1;
TestObj testObj = new TestObj();

for (int i=0; i<3; i++)
{
testObj = new TestObj();
testObj = populateObj(i+1);
System.out.println(testObj.s1); //this has correct value
myTestObjArray[i] = testObj;
}

//read array
System.out.println("Reading Array:");
for (int j=0; j<3; j++)
{
System.out.println(myTestObjArray[j].s1);
}

//test changing value in slot 0
testObj = new TestObj();
testObj.s1 = "This is a Test";
myTestObjArray[0] = testObj;
//display value in slot 2 (which somehow got populated in above)
System.out.println(myTestObjArray[2].s1);

return myTestObjArray;

}//end method

} //end class


Output when calling TestObj.buildObjArray()

The Number is 1
The Number is 2
The Number is 3
Reading Array:
The Number is 3
The Number is 3
The Number is 3
This is a Test

Even just at the end, where I explicitly assign "This is a Test" only to slot 0, it shows up in slot 2.



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40


Hint marked below....

eileen keeney wrote:I have a bug in the below, that causes all of the array slots to be populated with the same value.

I have successfully populated arrays of objects in the past using this same technique. But now I can not get this to work.

if I populate array[0] the value shows up in array[1] and array[2] as well.


Code:

public class TestObj
{
public static int i1;
public static String s1;

//----------------------------------------------------
public static TestObj populateObj(int iIn)
{
TestObj testObj1 = new TestObj();
testObj1.i1 = iIn;
testObj1.s1 = String.format("The Number is %2d", iIn);
return testObj1;
}//end method
//----------------------------------------------------

public static TestObj[] buildObjArray()
{
TestObj[] myTestObjArray = new TestObj[3];
String s1;
TestObj testObj = new TestObj();

for (int i=0; i<3; i++)
{
testObj = new TestObj();
testObj = populateObj(i+1);
System.out.println(testObj.s1); //this has correct value
myTestObjArray[i] = testObj;
}

//read array
System.out.println("Reading Array:");
for (int j=0; j<3; j++)
{
System.out.println(myTestObjArray[j].s1);
}

//test changing value in slot 0
testObj = new TestObj();
testObj.s1 = "This is a Test";
myTestObjArray[0] = testObj;
//display value in slot 2 (which somehow got populated in above)
System.out.println(myTestObjArray[2].s1);

return myTestObjArray;

}//end method

} //end class


Output when calling TestObj.buildObjArray()

The Number is 1
The Number is 2
The Number is 3
Reading Array:
The Number is 3
The Number is 3
The Number is 3
This is a Test

Even just at the end, where I explicitly assign "This is a Test" only to slot 0, it shows up in slot 2.

eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
Thank you,

That worked.

I have no idea why using public static would cause the type of error it caused.

But changing it fixed the problem.

Then I looked more closely at my past code that had no issues with objects, and noticed I had not used static except in the method definitions.



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

eileen keeney wrote:Thank you,

That worked.

I have no idea why making the variable public static would cause the type of error it caused.


But changing it fixed the problem.



Question: do you know what it means for a field variable to be static?
eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
Henry Wong wrote:
eileen keeney wrote:Thank you,

That worked.

I have no idea why making the variable public static would cause the type of error it caused.


But changing it fixed the problem.



Question: do you know what it means for a field variable to be static?


I do now, but I didn't until a few minutes ago when I looked it up.
 
Don't get me started about those stupid light bulbs.
 
subject: Populating an array of objects, bug?