Well, all is in the title...
I understand that both forms refer to the method, but I don't see what #
adds to the other form.
Well, all is in the title...
I understand that both forms refer to the method, but I don't see what #
adds to the other form.
The hash character (#) is not part of the Java language.
It has a special use in javadoc. The specification states
package.class#member is any valid program element name that is referenced -- a package, class, interface, constructor, method or field name -- except that the character ahead of the member name should be a hash character (#).
and
As stated, the hash character (#), rather than a dot (.) separates a member from its class. This enables the Javadoc tool to resolve ambiguities, since the dot also separates classes, nested classes, packages, and subpackages. However, the Javadoc tool is generally lenient and will properly parse a dot if you know there is no ambiguity, though it will print a warning.
The notation doesn't only apply to methods, it applies to any type member. It helps remove ambiguity from fully qualified type names.
The .
version is the formal notation used in the Java programming language, more concretely, consider the following code:
class Animal {
void fly() {
}
}
The usethe method fly()
we need to do the following:
Animal animal = new Animal();
animal.fly();
What we truly want to notate in the documentation however, is an invocation of the instance method fly()
, hence we denote Animal#fly()
, a seemingly alternative would be Animal.fly()
, but that would denote a static method of the Animal
class in Java code.
Hence Animal#fly()
is being used over all other forms, to summarize, alternatives would be:
Animal.fly()
, however this can mean a static method.animal.fly()
, however there is no class called animal
.As a sidenote, it is worth to note that since Java 8, we can actually do these things in Java code directly, by using:
Runnable animalFly = Animal::fly;
Here we also reference the Animal#fly()
method, however javadoc predates Java 8 by a long time.
MyType#myMethod()
is a syntax for JavaDoc
so that your documentation can link to a different method's documentation.
For example Object.equals()'s javadoc contains text to link to the Object.hashCode() method since they are related.
/**
* ...
*
* @see Object#hashCode()
*/
public boolean equals(Object obj)
Where as MyType.myMethod() is the syntx in actual syntax usd in Java code to call the method.