In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Why add the final keyword, in view of this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
During the development process, we may get used to some features of a programming language for customary reasons, especially if only one language is used for daily development. But when you develop in more than one language, you will find that although they are all high-level languages, many of their features are not quite the same.
Phenomenon description
Before Java 8, anonymous inner classes reported an error and prompted "Cannot refer to a non-final variable arg inside an inner class defined ina different method" when using external members:
But after Java 8, similar scenarios are no longer prompted:
Is it possible that such variables can be changed at will? Of course not. When you try to modify these variables, you will still get an error:
As you can see, when trying to modify a variable of the basic data type, the compiler's warning becomes "Varible 'num' is accessed from within inner class, need to be final or effectively final", which, unfortunately, still cannot be modified. By contrast, Kotlin does not have this restriction:
Cause analysis
Of course, there is no reason on the surface. Look at what the compiler has done. Several .class files are generated after running the javac command:
It is not difficult to infer that this TestInnerClass$1.class is the compiled file of the anonymous inner class to see what it is decompiled:
It turns out that anonymity will also be treated as a normal class, but when the compiler generates its constructor, it not only passes a reference to the external class, but also copies a copy of the variable of the basic data type. and pass the variable reference to the data type. Therefore, of course, the variables of the basic data types cannot be modified, otherwise they will be inconsistent with the external variables, and the transfer of the variables will become meaningless.
Scene comparison
But why is it possible for Kotlin to directly modify the values of basic data types in anonymous inner classes? Check out the decompiled content of Kotlin:
As you can see, when you need to pass variables of the basic data type, the Kotlin compiler wraps the data from value passing to reference passing, so that internal changes certainly do not affect the outside.
Verify how the Kotlin compiler handles when variables are not passed:
The answer to the question about why to add the final keyword is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.