Java anonymous internal class js closure, javajs

Source: Internet
Author: User

Java anonymous internal class js closure, javajs

Recently, when I saw closure (closure) in Javascript, I thought that both of java's anonymous internal classes involved variable/parameter references.

Let's first talk about the anonymous internal class of java. I will not explain its definition much. For more information, see the address.

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

. Today, we mainly talk about:

1. Why cannot an anonymous class access local variables in its enclosing scope that are not declaredfinalOr wait tively final.

Why must the local variables used in java anonymous internal class methods be defined as final or not modified (actually final)

2. What is the closure of js and what is the relationship with parameter reference?

Before explaining these two problems, we must first understand several concepts: scope and life time of variables ). Scope is for the block (block, curly brackets) containing variables during the compilation period. The life cycle refers to the entire period from memory allocation to release of variables when the program is running.

Generally, local variables are distributed on the stack. After the method is run, the memory occupied by local variables is released as the stack collapsed.

Let's talk about the second problem first:

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, the function can directly read global variables, but local variables in the function cannot be read outside the function. For various reasons, we sometimes need to get local variables in the function. However, as we have already said before, this cannot be done in general. It can only be implemented through a work und. This mechanism in js is called closure ). The above code is a closure. In my understanding, a closure is a function that can read the internal variables of other functions (the English document may be more accurate ). Through the closure, in the code above, the lifetime of the local variable status is extended, and its memory is not released with the completion of the quo () function. The underlying implementation mechanism of closure may be similar to that of closure.

Then let's 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, local variables in the method are released after the method is executed. When a local variable is used in java anonymous internal class methods, the copy of the variable instead of the variable itself is used. Therefore, after the method is executed, the copy of this local variable is not released, and the copy of this local variable is not allocated to the stack of this method, but to the upper pair, thus extending his life time. (See the preceding java code example) the reason why the final variable is required is that the local variable is immutable, because when the local variable I changes, while r has returned an anonymous internal class, r obtains the copy of the original I value (I = 0), and I is worth changing. r does not know, the existence of r does not have much practical significance, which is obviously inappropriate. To overcome this problem, java requires that the local variables used in java anonymous internal class methods cannot be modified (that is, they must be defined as final type, it is either not modified in the method), so that the local variables in the method are consistent with the local variables used in the java anonymous internal class method. Is there any feeling of js castrated closure ~

Note: The reason for putting these two questions together is that they have the same idea in solving the problem. It doesn't mean that java and js have many similarities. Even with the closure, we can see a big difference between the js and java languages. The internal functions in the js closure access the local variables themselves, the local variables used in java anonymous internal class methods are copies of local variable values. I hope this article does not give you such misunderstandings similar to JavaScript and java.

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.