Why do the best techniques vary for static classes in OOP?

I am currently reading about Java best practices and I found that according to this book we should be using static classes over non-static ones. I remembered that in C # best practices, we should avoid such as in the C # 3.0, 4.0 and 5.0 Coding Guide by Dennis Doomen:

AV1008 - avoid static classes

With the exception of extension method containers, static classes very often lead to poorly designed code. They are also very difficult, if not impossible, to test in isolation, unless you want to use some very hacky tools. Note. If you really need this static class, mark it static so that the compiler can disallow instances of instances and instantiate your class. This frees you from creating an explicit private constructor.

I found these two for C # answer and Java answer when to use and avoid static classes, but just by curiosity - both C # and Java are OOP languages, why is this a complete difference in best practices?

Update: I cannot copy so many pages from the Java book, but the bottom line is:

If you declare a member class that does not require access to a wired instance, always put the static modifier in its declaration, making it static and not a non-static member class. If you omit this modifier, each instance will have an external reference to its own enclosing instance. The storage of these reference costs is time and space and may result in the enclosing instance being retained when it would otherwise qualify for garbage collection (clause 6). And if you ever need to allocate an instance without a wrapper instance, you cannot do so, since non-static instances of a member class must have an enclosing instance. A common use for private static member classes is to represent components an object, represented by their enclosing class.

So this is just performance?

Please note that this question is about static classes and OOP, not the differences between Java and C #.

+3


source to share


2 answers


The advice given by JoshuaBloch also applies for C #, and the guidelines given for C # also apply for java (insofar as they talk about static classes).

Why use static members?

  • They don't need an instance to call
  • In C #, they use call code that doesn't need to be checked against null

    (which is micro-optimization), unlike instance methods (which uses callvirt opcode ). I believe there will be a similar thing in java.
  • They don't interfere with anything from the GC when the instance is not in use.
  • There is also no overhead of passing the this

    link to all hidden methods.

If you are used to Resharper's productivity tool for visual studio, it will give the same advice as JoshuaBloch for java, in C # that Method can be made static , which is justified in the given link.



Why not use static members?

  • They are not tested (easy).
  • They cannot implement an interface element.
  • They cannot be injected through dependency injection.
  • They do not participate in polymorphism (which is very necessary in object-oriented languages).
  • In C #, static classes cannot be passed as references.

So, both tips are good if you understand them and they apply for both languages. Use them when they are appropriate and avoid them when they are not.

+3


source


  • I believe the second piece of text is member class

    and enclosing

    , which is a Java specific feature (how is it implemented).
  • A static class or field (like in C #) is really bad design practice - take a look at OOP and SOLID. It can also lead to some performance issues at the CLR level. But this has nothing to do with a private static method. As far as I remember, Resharper advises making private methods independent on specific instances static. It improves readability and has no side effects. And you don't need a private unit test method. This is bad practice.
  • Finally, some authors write about technology in a nutshell, and some write about design principles. There is often a contradiction between these points of view (but the two fragments are not about that).


+3


source







All Articles