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?
Asked
Active
Viewed 5,185 times
31
Uyghur Lives Matter
- 17,261
- 40
- 105
- 135
Kevin Welker
- 7,446
- 1
- 37
- 55
-
3Dot 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 Answers
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.methodis 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
-
4This, 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
-
-
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