S
S
siRius322015-11-01 12:01:36
Java
siRius32, 2015-11-01 12:01:36

What is stored in the field?

There is a class "A", which in its method passes itself to the constructor of another class "B". Class "B" has a field for class "A". Class "B" in its method passes a field with class "A" to the constructor of class "C", class "C" also has a field for class "A". Class "C" further passes the field with the class "A" to the class "D", etc. What is stored in the fields "a", the same one object? Or are copies made? And is it correct to pass so far through all classes?

public class classA  {
    classB b;
    publc void init(){
        b = new classB(this);  
    }
}

public class classB  {
    classA a;
    classC c;
    publc classB (classA a){
        this.a = a;
    }
    publc void init(){
        c = new classC(a);  
    }
}

public class classC  {
    classA a;
    classD d;
    publc classC (classA a){
        this.a = a;
    }
    publc void init(){
        d = new classD(a);  
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nirvimel, 2015-11-01
@nirvimel

What is stored in the "a" fields, the same one object?

Yes.
Copies can only be created via clone() or via serialization+deserialization .
There is nothing criminal in passing a parameter through a constructor and storing one object in the field of another.
How much this is justified in a particular situation depends on the task that you are solving. It may also turn out that your entire class system is inappropriate for solving some problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question