Question about the external reference of anonymous internal classes and local internal classes must be final

Source: Internet
Author: User

After thinking for a long time, I still cannot come up with an accurate answer. If anonymous internal classes and local internal classes exist, they are invisible to the methods of peripheral classes, but they cannot be used by methods, it cannot be explained that internal class methods and variables can still be accessed, as long as an instance is created. Therefore, the problem can be solved as follows: 1. What if the internal class reference is not final? This problem has been explained since the reference is final. Because final cannot modify the object pointed to by the reference, if it is not final, the object pointed to by the reference can be modified. Now again: 2. Why cannot the reference be modified? What if I modify it? We all know that the alias phenomenon makes reference to the same object. If the reference is modified, the other reference will also be modified, which is very bad in some cases, but isn't this the case for internal classes? But we have never thought about using final, so it can only be the characteristics of the internal class, so that it must be done in this way. As mentioned above, internal classes are invisible. Is this invisible so that they must do this? 3. I found a problem. If the reference to be passed in is changed to static, it is acceptable to use this reference in the internal class method. However, to pass in the reference, you still need to use final, therefore, the key to this problem lies in the nature of reference. 4. What is the relationship between static reference and final reference? There is no association. Because static references do not need to be associated with a specific object, internal classes can be used instead of passing in the reference through methods. So the problem goes back to the origin, and how to explain it at the second point. The internal class should be regarded as a class method at a level. Therefore, it is the same as its method at the class level. Therefore, the reference cannot be modified. Is it to ensure the correctness of the reference? Because the reference passed by a method is more just a reference to an internal class, the method itself does not need this reference but must pass in parameters, but the reference may be modified in this method, so is this to ensure that the internal class can get the reference correctly? Therefore, the method parameter passing mechanism is involved. There are only two methods for passing parameters in Java: passing references and passing values. If a value is passed in, the object is copied and referenced, which means that the object is directly transmitted in, because any action made to the reference will directly affect the object. I think this explanation should be the most reliable at present. Because other internal classes do not have such requirements, only local internal classes and anonymous internal classes have such requirements. Other internal classes can directly include the members of peripheral classes as references, it does not need to go through the external method layer, so should it be true? There are a lot of inexplicable explanations on the Internet, but I can still find the same explanation as my answer later. He said it was to prevent the variable value errors in closure sharing. I think this is the same as my discussion above. The links are the same as those in the blog garden. Article : Http://www.cnblogs.com/chenjunbiao/archive/2011/01/26/1944417.html

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.