I have a class Class1 , in which I store some of my information.

In the class of активити А I create an instance of my class Class1 cl1 = new Class1() , fill in the data and pass this link to the class of the next ActivityB actB.cl2 = cl1 .

In the cl2 class активити B the cl2 field cl2 static, and access to it is inside the package. Активити А will be destroyed by the system sooner or later. Then what will happen to the field cl2 ?

    1 answer 1

    In Java everything is passed by value without exceptions. In this case, the object reference will be passed by value. When Активити А destroyed with the object referenced by cl1 , nothing will happen, since the cl2 object from Активити B will refer to it.

    The object will be deleted by the garbage collector ( GC ), if it is not accessible from the so-called root objects, these are objects that are accessible outside the Heap memory. Such objects include all local variables from the stack, active threads, loaded classes, and others . In the process of searching for garbage, the GC starts a detour from the root objects and gradually descending the tree of objects marks them as "living". Accordingly, if the object is not accessible from any root object, then it will be deleted.

    • Can you elaborate on the last sentence? - Uliyan Romanov
    • Slightly supplemented the answer - Pavel Parshin