Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Convention for using javadoc along with a method annotation? [duplicate]

Tags:

I have the following method:

   @Override
   public boolean myMethod()
   {
      // do stuff
   }

If I want to add a javadoc for this method, is there any convention on how to do this along with having the @Override annotation (or any other annotation)?

The reason I ask is because I've read that javadoc comments MUST be directly before the method (no newlines between the two), and I'm not sure if putting the javadoc comment directly above the @Override annotation would mess things up.

Would this be the conventional way to do it for instance? Does this work?

   /**
    * This is my method's javadoc comment.
    */
   @Override
   public boolean myMethod()
   {
      // do stuff
   }
like image 881
Tim Avatar asked Mar 25 '12 20:03

Tim


People also ask

Does Javadoc go before or after annotations?

If an annotation precedes any of the definitions listed above, then the javadoc comment should be placed before the annotation.

How do you add a Javadoc to a method?

Place the caret at the declaration in the editor, press Alt+Enter , and select Add Javadoc from the list.

How do you write a Javadoc comment for a method?

Writing Javadoc Comments In general, Javadoc comments are any multi-line comments (" /** ... */ ") that are placed before class, field, or method declarations. They must begin with a slash and two stars, and they can include special tags to describe characteristics like method parameters or return values.

What does @SEE mean in Javadoc?

In short, we use the @see tag when we want a link or a text entry that points to a reference. This tag adds a “See Also” heading to the reference. A document comment can hold any number of @see tags, all of which can be grouped under the same heading.


2 Answers

Yes, this way is the right way, I didn't see yet the other way around. And yes, this way it works. Didn't try the other way around.

   /**
    * This is my method's javadoc comment.
    */
   @Override
   public boolean myMethod()
   {
      // do stuff
   }

But basically I usually wouldn't add a javadoc comment to a method that overrides another method, especially when implementing interfaces. The tag @inheritDoc is helpful here, to distribute the documentation without big efforts. But that is specific to this example, you might add other annotations, too.

like image 96
Markus Avatar answered Sep 19 '22 11:09

Markus


Yes, it will work correctly. For example, in the source code of java.lang.String from openjdk, they are using javadoc on top of the @Deprecated annotation.

like image 27
Tudor Avatar answered Sep 18 '22 11:09

Tudor