Why do local variables accessed by inner classes (anonymous inner classes) in Java need to be final decorated?

Source: Internet
Author: User

This article mainly records: in Java, the (local) inner class accesses a local variable, why must this local variable need to be decorated with the final keyword?

First, what is a local variable? The local part here is the variable defined in the method .

Thus, the inner class is able to access a local variable, stating that the inner class is not an inner class defined in the class , but rather an inner class defined in the method , called a local inner class.

The scope of a local variable: a local variable is defined in a method, and when the method executes, the local variable disappears. "Local variables are allocated in the JVM's virtual machine stack, which is automatically reclaimed as the program executes", that is, the scope of the local variable is within the scope of the method.

However, when a local variable is accessed by the (local) inner class, the scope of the local variable is expanded. Look at one of the following examples:

1  Public classTest {2 3      Public Static voidMain (string[] args) {4         FinalString str = "Hapjin";5         6         NewThread (NewRunnable () {7 @Override8              Public voidrun () {9                 Try {TenThread.Sleep (50); One}Catch(interruptedexception e) { A e.printstacktrace (); -                 } -                  for(inti = 0; I < 10; i++) the System.out.println (str); -             } - }). Start (); -          +System.out.println ("Main thread finished"); -     } +}

① Line 4th defines a local variable str in the Main method, line 6th defines a local inner class thread, and accesses str in the local inner class thread

When the program executes to line 19th, the main () method ends, namely: the main thread ends. The life cycle of the local variable str should also end.

However, the thread thread is not finished yet, and the value of the local str can be printed in thread threads. This indicates that the local variable str scope is enlarged .

Therefore, if the local variable is not final decorated, we can arbitrarily modify The local variable value in the (local) inner class, and we can see the modified values outside the scope range of the local variable. This can cause some problems (I don't know what the problem is, security?). )

Therefore, Java specifies that local variables accessed by the (local) inner class must be final decorated to prevent changes to the value of the local variable.

Original: http://www.cnblogs.com/hapjin/p/5744478.html

Why do local variables accessed by inner classes (anonymous inner classes) in Java need to be final decorated?

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.