10

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.

mins
  • 6,478
  • 12
  • 56
  • 75
  • 2
    The former's the Javadoc form, if memory serves. I've tried to use it to mean "the method defined in `MyType`", while the dot is more used for actual method invocations, but I'm not really sure what the "true" meaning is. – awksp Jun 22 '14 at 19:28

3 Answers3

11

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • @mins I guess you could do that, but I don't see much point. You can't have both a `static` and non-static method with the same signature, so distinguishing them that way is not a big deal. – Sotirios Delimanolis Jun 22 '14 at 21:42
10

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.

skiwi
  • 66,971
  • 31
  • 131
  • 216
  • I feel like your answer isn't clear enough. Are you saying `#` is only used in documentation? Are you saying it's used to denote **instance** methods? Why bring up method references? – Sotirios Delimanolis Jun 22 '14 at 20:01
  • Thanks. To sum up: Animal#fly() and Animal.fly() denote respectively, for differentiation purpose in general documentation, an instance and a static invocation. And, mentioned by other posters: Javadoc also uses # to disambiguate the member name from its qualifiers, but without static or instance meaning. – mins Jun 22 '14 at 21:12
  • so, what about static method? if `fly()` is a static method of the class `Animal`, then we still denote it `Animal#fly()` in javadoc? – starriet Nov 13 '20 at 00:49
2

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.

dkatzel
  • 31,188
  • 3
  • 63
  • 67