Search code examples
javajavadocstatic-initializer

How can I add javadoc to a static initializer in Java?


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


Solution

  • First and foremost, it's arguable if static initializers are good practice to start with.

    If you decide to use them nevertheless, I'd add the documentation to the JavaDoc at the class level. Static initializers can, depending on how they're implemented, cause side-effects. If you use static initializers with side-effects, the behaviour should be documented for the consumer of said class.