hi,
I am just start learning java and i got this problem
I tried to create a class called testing and in the main file i create
2 object classes, but they are always linked togeter.
any one can help me ?
thanks
Jerry
public class testing
{
private static String internal[][];
public testing()
{
internal = new String[100][3];
}
public testing(int one)
{
internal = new String[100][3];
}
public void add()
{
internal[0][0] = "Me me me and me";
internal[1][0] = "Me me me and me";
internal[2][0] = "Me me me and me";
internal[3][0] = "Me me me and me";
}
public void minus()
{
internal[0][0] = "Mdme";
internal[1][0] = "Mwe";
internal[2][0] = "Mde";
internal[3][0] = "Md";
}
public static void displayTable()
{
int row;
int rowNow = 4;
for (row = 0; row < rowNow; row ++)
{
System.out.println (internal[row][0]);
}
}
}
---main class
testing label1 = new testing();
testing label2 = new testing(1);
label1.add();
label1.displayTable();
System.out.println("----------------");
label2.displayTable();
---the result:
Me me me and me
Me me me and me
Me me me and me
Me me me and me
----------------
Me me me and me
Me me me and me
Me me me and me
Me me me and me
Andrew Thompson - 10 Mar 2004 05:12 GMT
> I am just start learning java and i got this problem
> I tried to create a class called testing and in the main file i create
> 2 object classes, but they are always linked togeter.
> any one can help me ?
Uggh..
http://www.catb.org/~esr/faqs/smart-questions.html#prune
Try my variant and see if it makes
it more clear. Note that it now
includes a main, and has a more
sensible name that follows the
Java naming convention (class names
start with capital letters)
<code>
public class ArrayTest
{
private static String internal[][];
public ArrayTest()
{
internal = new String[100][3];
}
public ArrayTest(int one)
{
internal = new String[100][3];
}
public void add()
{
internal[0][0] = "Me me me and me";
internal[1][0] = "Me me me and me";
internal[2][0] = "Me me me and me";
internal[3][0] = "Me me me and me";
}
public void minus()
{
internal[0][0] = "Mdme";
internal[1][0] = "Mwe";
internal[2][0] = "Mde";
internal[3][0] = "Md";
}
public static void displayTable()
{
int row;
int rowNow = 4;
for (row = 0; row < rowNow; row ++)
{
System.out.println (internal[row][0]);
}
}
public static void main(String args[])
{
ArrayTest at1 = new ArrayTest();
at1.add();
at1.displayTable();
ArrayTest at2 = new ArrayTest();
at2.minus();
at2.displayTable();
}
}
</code>

Signature
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
FISH - 10 Mar 2004 18:15 GMT
> hi,
>
[quoted text clipped - 9 lines]
> {
> private static String internal[][];
^^^^^^
You have made the array static, meaning that it 'belongs' to the class
itself, not to each individual instance of the class. You perhaps
imagined that each instance of your class had its own String array
called internal - but in fact the static modifier caused them to
share a single array, so changes were reflected across all instances.
Simply remove the keyword static from the array defintion, and it
should work as you expect.
Btw: consider taking the advice offered elsewhere in this thread, and
use standard Java naming conventions.
-FISH- ><