Java anonymous inner class JS closure

Source: Internet
Author: User
Tags closure

Recently in the look at JS, see Closure (closures) This together, I think of the anonymous inner class of Java both have a reference to the variable/parameter problem.

First of all, the anonymous inner class of Java, his definition I will not do more explanation, you can refer to the address

Http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

。 Today we mainly say:

1, why cannot a anonymous class access local variables in it enclosing scope that is not declared as final or Effectivel Y final.

Why the local variables used in the methods of the Java anonymous inner class must be defined as final or unmodified (actually final)

2, what is the closure of JS, and the parameter reference and what is the relationship between

Before explaining these two issues, we must first know that several concepts are scope (scope) and lifetime (life cycle) of variables. Scope is for the compile period, which contains the variables within the block (blocks, curly braces). And the life cycle refers to the program at run time, the variable in memory from the allocation of space to release the entire period.

In general, local variables (locally variable) are allocated on the stack (stack), and as the stack collapses after the method runs, the memory occupied by the local variable is freed.

So let's start with a second question:

var quo = function (status) {
return {
Get_status:function () {
return status;
}
};
};
Make an instance of quo.
var myquo = Quo ("Amazed");
Document.writeln (Myquo.get_status ());

For JS, a global variable can be read directly inside the function, and local variables within the function cannot be read naturally outside the function. For a variety of reasons, we sometimes need to get local variables within the function. However, as already mentioned, in general, this is impossible, only through the workaround can be achieved. This mechanism in JS is called closure (closure). The code above is a closure, and I understand that closures are functions that can read variables inside other functions (the interpretation of English documents may be more accurate). With closures, the lifetime of the local variable status is extended in the above code, and its memory is not freed with the completion of the function quo (). The underlying implementation mechanism may be similar so that the local variables captured by closure are placed on the heap rather than on the stack.

Then talk about the first question:

Public Runnable f (int x) {int i = 0; Runnable r = new Runnable () {@Overridepublic void run () {System.out.println (i); i = 10;}} i = 100;return r;}

In Java, in general, after the method executes, local variables within its methods are also released. When using local variables in the methods of the Java anonymous inner class, the copy of the variable is used instead of the variable itself, so the copy of the local variable is not freed after the execution of the method, and the copy of the local variable It is not assigned to the stack of the method, but is assigned to the upper, thus prolonging his lifetime. (See the Java code example above) and the reason for the requirement that the variable is final is to ask that the local variable is immutable, because when the authority variable I changes, and the anonymous inner class R has returned, and R gets the I original value (i=0) of the copy,i later worth change, R is not known, The existence of R does not have much practical significance, which is obviously inappropriate. So in order to overcome this problem, Java requires that the local variables used in the methods of the Java anonymous inner class must not be modified (that is, either defined as final type, or not unmodified within the method). In this way, local variables in the method are consistent with the local variables used in the methods of the Java anonymous inner class. There is no kind of JS castration version of the feeling of closure ~

Note: The reason for putting these two issues together is that they have the same ideas on how to solve the problem. Not that Java and JS have a lot of similarities. Even in the closure of the problem, you can see the two languages JS and Java a large difference between the language, JS closure inside the function access is the local variable itself, and the Java anonymous inner class method used in the local variable is a copy of the value of the local variable. I hope this article does not give you this kind of JS and Java similar misunderstanding.

More about JavaScript's closure can be seen: http://www.jb51.net/article/24101.htm

Java anonymous inner class JS closure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.