Please explain if the object will be deleted if there is a handler:

public class Util { static void attachHandler(Node node) { node.setOnMouseClick(me -> { .... } )}; } class SomeClass { Node node=new Node(); public SomeClass() { Util.attachHandler(node); } } class Progr { List<Node> list = new ArrayList<>(); public static void main(String[]args){ list.add(new SomeClass()); // Создан экземпляр SomeClass и обработчик MouseClick для node list.remove(0); // Ссылка на экземпляр SomeClass стала недоступна. // А удалился ли обработчик? // Не держит ли он ссылку на SomeClass? // И зачистит ли все коллектор? } } 

Thank.

    1 answer 1

    The relationship between objects can be described as:

     [ list ] -> [ someClass ] -> [ node ] -> [ me -> {} ] 

    From this it follows that the removal of any object from the presented chain will automatically lead to the removal of all objects located to the right of the deleted one.

    There are no circular references, neither Node, nor lambda contains references to the SomeClass object, but even if one of them contained such a link, the GC would still delete the chain of objects referencing each other, this distinguishes the GC from Automatic Reference Counting .

    • Thanks for the explanation. - micom