Why is the outer variable used in the inner class in Java final decorated?

Source: Internet
Author: User

So final is syntactically constrained by the fact that two different variables are actually consistent (represented by the same variable).

My understanding:

For example, this code:

 Public void Test (String str) {  str="ABC";  }

In fact, the execution is complete, only the original STR image file, do not affect the original variable.

An inner class is a component that belongs to an external class

classouter{PrivateString str; classinner{ Public voidTest () {System. out. println (str); }    }   Public Static voidMain (string[] args) {Outer.Innerinch=NewOuter ().NewInner ();  This. str="AD"; Thsi.str="BC"; }      }

I think that if the variables of the outer class are not final, then after the first generation of the inner class object, the result of the method execution of the inner class will change with the variable of the external class. However, the variable should be a component of the inner class, which means that you can change the inner class object generated by the arbitrary manipulation of the outer class object, which is obviously wrong. So we'll define the outer class object as final, which guarantees the consistency of the performance of the outer class and the inner class.

Not very understanding. Original link:

http://blog.csdn.net/axman/article/details/1460544

Why is the outer variable used in the inner class in Java final decoration?

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.