I have 2 classes, A and B. A contains properties that I want to use in B.
I want to put all objects I initialize in B into an array every time a new object is initialized. With this code, the first object should go to array-position 1, the second object to array-position 2 and so on. The variable n basically determines to which position the object should go.
I know that with "this." I can access individual variables from this object (as you can see with x and y), but I don't know how I can access the object as a whole. You see that I have a comment in my code, which shows what I try to do. It doesn't work, because it's not the proper syntax.
What do I have to put there so it will work?
class A{
private int x, y;
private static int n;
A(int x, int y){
this.x = x;
this.y = y;
n++;
//B.object[n] = this.object;
//I tried this but it doesn't work
};
}
class B{
public static A[] object = new A[10];
public static void main(){
A object1 = new A(1,2);
A object2 = new A(3,4);
A object3 = new A(5,6);
};
}
objectis not defined in classAhence the compiler error.B.object[n] = this;public static void main(String[] args)if you want your program to execute