In a recent code review, a developer was advised against using static method imports. This sparked a discussion about the appropriate use cases for this feature.
One concern raised by the reviewer was the potential confusion caused by unqualified method calls appearing to belong to the current class. While this may be an issue in some cases, the official Java documentation from Sun advises using static imports sparingly, only when tempted to "abuse inheritance" or to avoid declaring local copies of constants.
A viable use case for static imports is when a method from another class is frequently used in a particular context. For example, it may make sense to import the save() method from a DA class that provides mostly static methods. However, it is important to import individual members, such as static some.package.DA.save(), rather than DA.*, to maintain clarity.
Another consideration is the potential for ambiguity. If the unqualified call could be interpreted as belonging to the current class, it may be preferable to extend the superclass or avoid static imports altogether.
Ultimately, the decision of whether or not to use static method imports is personal, but it should be made carefully, considering the potential drawbacks and benefits outlined above.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3