Java – why are the best practices for static classes different in OOP?

I'm currently reading about Java best practices. I find that according to this book, we must support non static static classes I remember that in c# best practice, we must avoid this situation according to Dennis doomen's c#3.0, 4.0 and 5.0 coding guidelines:

I found these two times in c# answer and Java answer to avoid static classes, but just out of curiosity - c# and Java are OOP languages. Why is it completely different from best practices?

Update: I can't copy so many pages from a java book, but the bottom line is:

Is it just about performance?

Please note that this problem is more about static classes and OOP than the difference between Java and c#

Solution

Joshua Bloch's advice also applies to c#, and his advice also applies to c# (when talking about static classes)

>They don't need instances to call them > in c#, they use call opcode, which doesn't need to check null (which is a micro optimization) instead of instance methods (using callvirt opcode) I believe similar things will also appear in Java. > If you don't use instances, they won't block something in the GC. > There is also no overhead of passing this reference to all hidden methods

If you are used to the resharper productivity tool of visual studio, it will provide the same suggestions as those given by Joshua Bloch. In c#, it says that method can be made static is reasonable in a given link

>They are not easy to test (easy). > They cannot implement interface members. > They cannot be injected through dependency injection. > They are not involved in polymorphism (much needed in object-oriented languages). > In c#, static classes cannot be passed as references

Therefore, if you understand them and they apply to both languages, both suggestions are good Use them when appropriate and avoid them when they are not suitable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>