4

Prior to Java 8, my observation was that the de facto standard for referring to member methods was by using the # symbol (such as Object#toString()). Then along came Java 8 which instead chose the :: operator for method references for seemingly no reason.

Has there been an official explanation or justification for why :: in particular was chosen?

BambooleanLogic
  • 7,530
  • 3
  • 30
  • 56
  • Where has `#` been used to refer to member methods? – Kayaman Mar 06 '15 at 09:39
  • Java has no such thing as "member methods"; functions are not a first class citizen in Java. The `::` was simply borrowed to have a convenient way of defining a method reference, with the left part being a class or instance of it, and the right part being the method name. – fge Mar 06 '15 at 09:41
  • # was never part of Java API standard syntax for refering to methods. You might want to review http://stackoverflow.com/a/22245383/579580 to better understand :: – aviad Mar 06 '15 at 09:42
  • Why asking _"Has there been an official explanation or justification for why :: in particular was chosen?"_ is considered as **primarily opinion-based** ? – gontard Mar 06 '15 at 09:52
  • @gontard Beats me, but I find that somewhat amusing for some reason. I would ask "how does that qualify as opinion-based?", but then they would probably close this comment too for being primarily opinion-based. :) – BambooleanLogic Mar 06 '15 at 09:53
  • 7
    Nevermind... [there is an official explanation or justification](http://openjdk.5641.n7.nabble.com/Method-reference-double-colon-syntax-td58953.html) – gontard Mar 06 '15 at 09:57
  • 1
    @gontard: Thanks, that's exactly what I was looking for. If this question gets reopened, feel free to post that as an answer (with an excerpt or something to follow the no-link-only guidelines) and I'll happily accept it. – BambooleanLogic Mar 06 '15 at 10:03

1 Answers1

7

You could find an "official explanation or justification" from Brian Goetz. You should read the complete discussion but this is an extract :

The :: infix syntax:

   ClassName::methodName 
   ClassName<T>::methodName 
   ClassName::<U>genericMethodName 

works acceptably well. Some people like it, and some people hate it -- just like #. There's never going to be a perfect syntax for anything that makes everyone jump up in unison and say "yeah, that's it!" But :: is OK, and using up :: here is far better than using up #. (And, while this might look a little weird to C++ programmers, the overlap between the Java and C++ developer bases at this point is small enough that I don't think we should be too worried about that.)

gontard
  • 28,720
  • 11
  • 94
  • 117