How to add javadoc to static initializer in Java?

I edited the class and moved some code from the constructor to the static initializer. What should I do with the javadoc that was on the constructor? Is it possible to add javadoc to a static initializer?

+6
source share
4 answers

First of all, it can be argued that static initializers are a good practice to start with.

If you decide to use them, I will add documentation to the JavaDoc at the class level . Static initializers can, depending on how they are implemented, cause side effects. If you use static initializers with side effects, the behavior should be documented for the consumer of the specified class.

+5
source

JavaDoc is primarily intended to document the class interface. JavaDoc comments must precede the declaration of a class, field, constructor, or method.

A static initializer is not part of the interface. This is part of the class implementation.

If necessary, you can document its behavior in the class documentation.

+6
source

I would say that the important parts of this documentation should be transferred to the class documentation:

/** * Your text here. */ public class SomeClass { static { /* your static initalizer */ } } 
+2
source

There is no such thing as a static constructor in Java ( as shown in C # ) in Java, so you should document this behavior at the class level.

In addition, since the static initializer is likely to initialize some static fields, if these fields are public, protected (or private packages), depending on your JavaDoc visibility agreement), you add additional information about how these fields are initialized based on the behavior of the static initializer.

+2
source

Source: https://habr.com/ru/post/891207/


All Articles