Setting outer variable from anonymous inner class

后端 未结 9 867
臣服心动
臣服心动 2020-11-28 07:03

Is there any way to access caller-scoped variables from an anonymous inner class in Java?

Here\'s the sample code to understand what I need:

public L         


        
9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 07:55

    Java doesn't know that doWork is going to be synchronous and that the stack frame that result is in will still be there. You need to alter something that isn't in the stack.

    I think this would work

     final Long[] result = new Long[1];
    

    and then

     result[0] = st.getLong(4);
    

    in execute(). At the end, you need to return result[0];

提交回复
热议问题