Variable used in lambda expression should be final or effectively final

后端 未结 7 876
抹茶落季
抹茶落季 2020-11-22 07:54

Variable used in lambda expression should be final or effectively final

When I try to use calTz it is showing this error.

7条回答
  •  清歌不尽
    2020-11-22 08:28

    From a lambda, you can't get a reference to anything that isn't final. You need to declare a final wrapper from outside the lamda to hold your variable.

    I've added the final 'reference' object as this wrapper.

    private TimeZone extractCalendarTimeZoneComponent(Calendar cal,TimeZone calTz) {
        final AtomicReference reference = new AtomicReference<>();
    
        try {
           cal.getComponents().getComponents("VTIMEZONE").forEach(component->{
            VTimeZone v = (VTimeZone) component;
               v.getTimeZoneId();
               if(reference.get()==null) {
                   reference.set(TimeZone.getTimeZone(v.getTimeZoneId().getValue()));
               }
               });
        } catch (Exception e) {
            //log.warn("Unable to determine ical timezone", e);
        }
        return reference.get();
    }   
    

提交回复
热议问题