31

When writing about methods in Java (i.e. in forums, mailing lists, issue trackers, etc.) many people separate the method name from the class name using the '#' symbol instead of Java's native . operator; for example, folks refer to Object#toString instead of Object.toString. Where does this syntax come from?

Uyghur Lives Matter
  • 17,261
  • 40
  • 105
  • 135
Kevin Welker
  • 7,446
  • 1
  • 37
  • 55
  • 3
    Dot notation may be seen as code, where in Object.toString `Object` will be an instance or otherwise `toString` will be a static method. Dash notation is Javadoc and so is clear. – maksimov Jun 28 '12 at 15:36

1 Answers1

28

It's the notation used in javadoc comments when linking to another class' method.

EDIT

To gather the additional information provided in comments:

  • @Hugo notes that the # notation in turn comes from HTML anchors
  • @maksimov points out that Object.method is the Java syntax to call static methods, which could be misleading

UPDATE

Java 8 brings a new syntax for method references, which now seems to become more popular - so Object#toString tends to now be written Object::toString.

assylias
  • 310,138
  • 72
  • 642
  • 762
  • 4
    This, in turn, comes from HTML Anchors, which are represented using this notation, since each method is an anchor if the HTML javadoc in generated. – WhyNotHugo Jun 28 '12 at 15:34
  • all makes sense. I may start to use it now, but I have to admit it is quite ugly and I find it harder to read. But it does provide disambiguation in the case of static method invocations. – Kevin Welker Jun 28 '12 at 17:23
  • But what if the Object is lowercase? – Ruchir Baronia Apr 03 '16 at 17:38
  • Lowercase or uppercase is irrelevant. However based on javadoc, the class will be uppercase, an object instance lowercase: `Object obj`. When you want to refer to the method implementation you are supposed to use `Object#method`. Otherwise if you are talking about a specific call, then you can go with `obj.method()`. – Koenigsberg Sep 11 '18 at 10:52