In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to correctly use static methods in java". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use static methods correctly in java".
To know when and how to use static methods correctly, you must first figure out the difference between static methods and instance methods. Static (static) itself indicates that the behavior is stateless, does not need to be instantiated, and does not require extra memory allocation to hold the instance when called. Therefore, for global singleton scenarios, stateless behavior, you can consider using a static method. However, a fatal problem with static methods is that they are strongly coupled to specific types. If this behavior is likely to change, avoid using static methods.
Therefore, there are the following situations:
For determining tool behavior that will not change, use a static method
You need to provide syntax candy to make the code easier to read, and you can define a static method because it can be static import.
For example, there are some utility classes Files and Preconditions in google's common library. The methods provided by these utility classes usually do not change. For example, in the Preconditions utility class, the logic of non-null checking will not change in the future, and the behavior has no state, so you can use the static method as the tool method.
Public static T checkNotNull (T reference, @ Nullable Object errorMessage) {if (reference = = null) {throw new NullPointerException (String.valueOf (errorMessage));} else {return reference;}}
Preconditions.checkNotNull (name, "name is null")
For the second case, such as the need to use assertions in unit testing, in order to improve the expressiveness of the test code, efforts should be made to test more in line with natural language reading habits. The AssertJ verification framework based on Java is famous for its smooth interface. For example, the assertThat () method it provides, in order to better reflect the characteristics of DSL, is defined as a static method, and hides type information through static import, making method calls more natural:
AssertThat (fellowship) .extracting ("name") .extraction ("Boromir", "Gandalf", "Frodo", "Legolas")
If these two conditions are not met, try not to use static methods, because static methods are not good for extension, bad for Mock, and therefore bad for writing tests. As for some stateless service methods, such as the OnSalePolicy class encapsulated for the change of promotion strategy in e-commerce system, the static method can not be used because the promotion strategy changes at any time. The best choice is to define it as a service interface and then achieve loose coupling and embrace change through dependency injection (Dependency Injection).
These are all the contents of this article entitled "how to use static methods correctly in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.