Behavior of Functional Interface and Method Reference

前端 未结 2 1672
醉酒成梦
醉酒成梦 2021-01-13 14:10

What happens when the reference of a method which belongs to a variable is destroyed?

public class Hey{
    public double bar;

    public Hey(){
        bar         


        
2条回答
  •  半阙折子戏
    2021-01-13 14:41

    Scope is a compile time concept that governs where names in source code can be used. From the JLS

    The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name, provided it is visible (§6.4.1).

    The fact that the name hey is restricted to the body of the whatsGonnaHappen labeled statement doesn't have any bearing on whether the instance referenced by hey at runtime is a candidate for garbage collection (which I assume is what you're worried about).

    Concerning the variable capture, yes, the method reference hey::square is a reference to an instance method of a particular object (the one referenced by hey) and will therefore capture the value of the variable hey when the method reference expression is evaluated to produce an instance and use it when apply is invoked.

提交回复
热议问题