I'm trying to access a method of a class I defined that is in an array. Code:
public class Test1 {
private static Frame[] arr;
public static void main(String[] args) {
arr = new Frame[5];
for (int k = 0; k < 5; k++){
arr[k] = new Frame(k,k);
System.out.println(arr[k].getFrameNumber());
}
for (int k = 0; k< 5; k++){
System.out.println(arr[k].getFrameNumber());
}
}
}
the output is:
0
1
2
3
4 (good to this point, the end of the first for loop)
4
4
4
4
4 (wtf??? should match the top)
The Frame class is just a few integers, and they all have a default constructor (which would show up as -1 on the output), so I think I am initializing everything properly. I can't figure out why after I finish initializing the array, if I change one element, it changes the entire array. This behavior is counter to the examples and documentation I have seen so far, and also counter to my experience with arrays in c++.
EDIT: THe forum ate my [] bracket things. This is the only place I visit that doesn't have a code feature (and needs one badly)
public class Test1 {
private static Frame[] arr;
public static void main(String[] args) {
arr = new Frame[5];
for (int k = 0; k < 5; k++){
arr[k] = new Frame(k,k);
System.out.println(arr[k].getFrameNumber());
}
for (int k = 0; k< 5; k++){
System.out.println(arr[k].getFrameNumber());
}
}
}
the output is:
0
1
2
3
4 (good to this point, the end of the first for loop)
4
4
4
4
4 (wtf??? should match the top)
The Frame class is just a few integers, and they all have a default constructor (which would show up as -1 on the output), so I think I am initializing everything properly. I can't figure out why after I finish initializing the array, if I change one element, it changes the entire array. This behavior is counter to the examples and documentation I have seen so far, and also counter to my experience with arrays in c++.
EDIT: THe forum ate my [] bracket things. This is the only place I visit that doesn't have a code feature (and needs one badly)