5

Is there a shorthand way to get a method reference to a local static method the same way the this keyword or class prefix can be dropped when invoking a method?

The obvious thing would be to use ::myStaticMethod but that does not seem to compile:

class MyClass {
   static void myStaticMethod () {}
   static Runnable runner = ::myStaticMethod; // doesn't compile
      // requires MyClass prefix despite being in the same class
}
Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
  • 3
    I suspect the answer is "you can't do better than `MyClass::myStaticMethod`". – Oliver Charlesworth May 14 '17 at 20:22
  • @OliverCharlesworth: That's also what I suspect, but I have not found anything conclusive yet and was hence hoping for there possibly being something; or at least that someone would have some insight for instance into some JSR where this is being discussed. – Janick Bernet May 14 '17 at 20:24
  • It's mentioned here: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html (see the table). Presumably you can find this documented "officially" in the JLS (though I haven't checked!). – Oliver Charlesworth May 14 '17 at 20:25
  • @LewBloch: I don't think the tone of your comment is appropriate for this forum, especially given that your sarcasm is unfounded. Code readability is very subjective, I would personally argue dropping such prefix improves readability, but irrespective of our individual opinions, as clearly stated in my question, there is precedence in the form of method invocation where the designers explicitly chose to provide such shorthand yet apparently broke with that pattern when introducing method references. Surely others must have been wondering about this and will now find the answer here. – Janick Bernet May 15 '17 at 20:36
  • Comment deleted. I apologize for offending you. I must warn you, however, that I resort to rhetorical devices routinely, including sarcasm, not to insult anyone but to stimulate thought. If you are inclined to take offense at my "tone", you impose your standards on the message and likely will miss the point. There's nothing personal in my remarks except when I specifically use "you", as here with you now. People often work very hard to reduce keystrokes to ludicrous extent. Why? What is the fascination? Actually, carpal tunnel is a reasonable guess, isn't it? – Lew Bloch May 15 '17 at 21:23
  • [You are not the first one to ask for this](http://stackoverflow.com/q/30251867/2711488). I’m not going to vote for closing as duplicate as you asked primarily for *whether* it is possible, while the other(s) asked about *why not*, but I think, it’s useful to link these questions. Maybe it helps… – Holger May 18 '17 at 14:32

1 Answers1

5

Alas, there is no shortcut. According to the JLS (15.13), the grammar of method references is as follows:

MethodReference:
    ExpressionName :: [TypeArguments] Identifier 
    ReferenceType :: [TypeArguments] Identifier 
    Primary :: [TypeArguments] Identifier 
    super :: [TypeArguments] Identifier 
    TypeName . super :: [TypeArguments] Identifier 
    ClassType :: [TypeArguments] new 
    ArrayType :: new

In all cases, there's something before the ::.

This grammar is also discussed less formally in the Java tutorial on method references.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Thanks, that is the kind of authoritative answer I was looking for. Still hoping they might add a shorthand in a newer version, as it would make for slightly more concise code. – Janick Bernet May 14 '17 at 20:50
  • The manual is freely available on line for anyone​ to read who bestirs themselves to do their own research. – Lew Bloch May 14 '17 at 21:31