So, lets say I have a method that contains an annotation like so:
@Override
public void bar(String x)
If I were to add Javadoc comments to this snippet of code, which is the preferred method?
Either:
/**
* @param x A string lol
*/
@Override
public void bar(String x)
Or:
@Override
/**
* @param x A string lol
*/
public void bar(String x)
Also of interest here - the annotation is on the same line as the other qualifiers for the method. I've never seen that done before, but it seems to suggest that annotations should be treated much like other qualifiers for a method, and as such, the javadoc should definitely go before it.
javadoc is "inherited" - javadoc written for an interface method or an abstract method is automatically associated with corresponding concrete implementations. it's a common error to put the class level javadoc comment at the very start of a source file, before the import statements.
By annotating a text, you will ensure that you understand what is happening in a text after you've read it. As you annotate, you should note the author's main points, shifts in the message or perspective of the text, key areas of focus, and your own thoughts as you read.
reflect. Method. getAnnotation(Class< T > annotationClass) method of Method class returns Method objects's annotation for the specified type passed as parameter if such an annotation is present, else null. This is important method to get annotation for Method object.
First one. The annotation applies to the method, not the comment. It's also what most IDEs will do, so is the most common anyway.
Personally, I prefer the former (i.e. annotation "touching" the method signature), since then it's code with code.
But either works for the compiler, so it's down to personal taste/your organisation's coding standards.
Opinion: The first method is preferable. In a way the annotation and the method belongs together stronger than the comment.
Generally annotations are pit on the line (or lines) immediately before the method. Annotations can be a bit long to put on the same line.
However, @Override
is a bit special. It's effectively making up for the language not having override
. Conventionally it is placed on the same line (although you'll see plenty of examples where it isn't).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With