1

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);
        };
}
6
  • 1
    that's where the 'this' keyword comes in, but you don't have instances you can contact, because you are working with static methods. Commented May 4, 2017 at 19:38
  • object is not defined in class A hence the compiler error. Commented May 4, 2017 at 19:39
  • use B.object[n] = this; Commented May 4, 2017 at 19:41
  • Also, define main as public static void main(String[] args) if you want your program to execute Commented May 4, 2017 at 19:55
  • I don't intend to execute class A, that's why I don't need a main-method there. Commented May 4, 2017 at 20:35

2 Answers 2

1

Your object reference inside B class holds an array of A types, so you need to simply set B.object[n] = this; as shown below (Here, this itself represents the current A object, so no need to say this.object):

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;
    };
}



class B{
        public static A[] object = new A[10];
        public static void main(String[] args){
                A object1 = new A(1,2);
                A object2 = new A(3,4);
                A object3 = new A(5,6);
        };
}

Also, in Java, main method takes String[] as input arguments (shown above) which will be the method executed by the JVM at the start (though with main(), the code compiles, but JVM does not invoke it at the start).


But as a side note, remember that what you are trying do creates circular dependencies (class A depends on class B and then class B depends on class A), so avoid that.

Sign up to request clarification or add additional context in comments.

Comments

0

Although javaguy's solution solves your problem, it will probably cause the compiler to give a warning about a "leaking this" in the constructor.

This could be potentially dangerous in a multithreaded environment because it allows you to get hold of a reference to A (via B.object) before A has fully run through its constructor.

A better pattern in this case would probably be to have a static helper method in A that constructs those objects for you, adds them to the list and then returns a reference to them.

class A{
    private static int n;

    private int x, y;

    public static A createA(int x, int y){
        A result = new A(x, y);
        n++;
        B.object[n] = result;
        return result;
    }

    private A(int x, int y){
        this.x = x;
        this.y = y;
    }
}



class B{
        public static A[] object = new A[10];
        public static void main(String[] args){
                A object1 = createA(1,2);
                A object2 = createA(3,4);
                A object3 = createA(5,6);
        }
}

By making the constructor of A private, you ensure that new A(1,2) won't work from another class, so you have to use the helper method to get an instance.

1 Comment

javaguy actually solved my problem pretty well. I only learned that with "this." I can access object variables, without realizing that "this" is the object itself. I didn't knew that before. Also I tested javaguys code and it did exactly what I wanted.