You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Anonymous classes in java are close to being called as a closure. They don’t 100% support the definition but come close to it and thats why we see lot of literature calling anonymous inner classes as closure. Why do I say its not 100%? An anonymous inner class can access “only” the [final](https://javapapers.com/core-java/explain-the-final-keyword-in-java/) local variable of the enclosing method. It is because of this restriction, anonymous inner class in java is not a closure.
493
+
494
+
If you remember the [memory management in java](https://javapapers.com/core-java/java-jvm-memory-types/), you can recall that the local variables are stored in a stack. These java stacks are created when the method starts and destroyed when it returns. Unlike local variables, final fields are stored in method area which lives longer even after the return of the method. If we want to make anonymous inner class as a closure, then we should allow it to access all the fields surrounding it. But, as per the current memory management, they will be destroyed and will not be accessible after the method has returned.
495
+
496
+
## Closure in Java
497
+
498
+
In that case will we get closure in java in future? We have a specification written by Peter Ahe, James Gosling, Neal Gafter and Gilad Bracha on [closures for java](http://www.javac.info/closures-v05.html). It gives detailed description of how a closure can be implemented in java and example code on how to use them. We have JSR 335 for closures in java named as Lambda Expressions for the Java Programming Language.
499
+
500
+
```java
501
+
classClosureTest {
502
+
publicinterfaceMutableAdder {
503
+
intadd(intx, booleanchange);
504
+
}
505
+
506
+
publicMutableAddermakeAdderB(intn) {
507
+
// Variable 'intHolder' is accessed from within inner class, needs to be declared final
Variable 'a' is accessed from within inner class, needs to be final or effectively final(java 8)。
532
+
533
+
Java doesn't support closures, i.e. local variable can’t be accessed outside the method, but fields of class can be accessed from outside the class.
534
+
535
+
536
+
537
+
What are local variables in java?
538
+
539
+
All variables of the method are called local variables in java.
540
+
541
+
542
+
543
+
Where do local variables live in java?
544
+
545
+
[Methods are pushed on **stack**](http://www.javamadesoeasy.com/2015/03/threads-implement-their-own-stack.html) so local variables live on the stack.
546
+
547
+
Local variables of the method are kept on the stack and are lost as soon as the method ends in java.
548
+
549
+
550
+
551
+
Where do object of local inner class live in java?
552
+
553
+
As object of local inner class live on the **heap**, objects may be alive even after method ends in which local inner class have been defined.
554
+
555
+
**As, local variables of the method are kept on the stack and are lost as soon as the method ends,****but even after the method ends, the local inner class object may still be alive on the heap.**
556
+
557
+
558
+
559
+
What java docs says about “Local Inner class cannot access the non-final local variables but can access final local variables.”
560
+
561
+
**A local class can only access local variables that are declared final in java**. When a local class accesses a local variable or parameter of the enclosing block, it captures that variable or parameter in java.
562
+
563
+
564
+
565
+
JVM create create a synthetic field inside the inner class in java -
566
+
567
+
As final variable will not change after initialization, when a inner class access final local variables **compiler create a synthetic field inside the inner class and also copy that variable into the heap.** So, these synthetic fields can be accessed inside local inner class even when execution of method is over in java.
568
+
569
+
570
+
571
+
You must be wondering, **What are synthetic fields in java?**
572
+
573
+
Synthetic fields are created by compiler and they actually doesn’t exist in source code in java.
574
+
575
+
576
+
577
+
The reason is that after the enclosing method returns, the local variable no longer exists. Therefore a copy of the variable is created when the anonymous class is instanciated. If Java allowed the local variable to be changed afterwards, the anonymous class would only know the old value.
578
+
579
+
简单的说就是: JVM在内部类初始化的时候帮我们拷贝了一个局部变量的备份到内部类中,并且把它的值复制到了堆内存中(变量有两份,同样的名字,一个在局部变量中用,一个在内部类中)。所以要是不用final修饰,那你后面把外部类中的变量的值修改了,而内部类中拷贝的值还是原来的,那这样岂不是两边的值不一样了? 所以不能让你改,必须加final。The solution was to required that captured variables are final (before JDK 8) or effectively final (since JDK 8), which means they cannot be assigned to.
0 commit comments