Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating an array of objects, bug?

 
eileen keeney
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic