Sample code in javadoc comment

I have some programmers who give examples for javadocs, and some examples contain comments formatted with

/* * */ 

When I put these examples in a javadoc comment, the comment closes in the example, closes the javadoc comment.

 /** * * /* * * * */ <-- right here * */ 

Is there a right way to handle this without telling everyone that they cannot write comments in this format?

+4
source share
5 answers

Javadoc comments use html, so encode / as an object: &#47;

 /** * * /* * * * *&#47; <-- right here * */ 

Saying that everyone who has not commented on their code examples can be easier.

+10
source

In my opinion, if the code is not self-evident, or at least simple enough to understand with a brief description, then the code should be reorganized. It should be shorter, or variables should be more understandable, or logic needs to be rethought.

In any case, I donโ€™t think there is a way around this if you want to include an example and then you will not have comments in this example. If you really have comments, use the // notation.

+3
source

Why do you want to put a comment in the source code in a comment?

It is like something is wrong with your design if something like that is necessary.

0
source

HTML is allowed in Javadoc comments. Add code to your comment within the <code> or <pre> elements. For instance:

 /** * Outputs "Hello World" to the console. * * <code>System.out.println("Hello World");</code> */ 
0
source

"/ * * /" comments cannot be nested. Comments "//" may be, but they are valid only until the end of the line on which they begin.

Generally speaking, it is not good to include actual code in JavaDocs - firstly, they should be more abstract (โ€œwhyโ€ of things, not โ€œhowโ€).

0
source

All Articles