Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java local static method reference shorthand syntax

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
}
like image 913
Janick Bernet Avatar asked May 14 '17 20:05

Janick Bernet


People also ask

How do you reference a static method in Java?

Static Method They are referenced by the class name itself or reference to the Object of that class. public static void geek(String name) { // code to be executed.... } // Must have static modifier in their declaration. // Return type can be int, float, String or user defined data type.

How do I print a reference method?

How to print the values using method reference? in method references you just give the name of the function (println), they don't take arguments. You can create your own function that accepts a string and calls toUpperCase and then println, and then give the name of your function as the method reference name.

How do you pass parameters in a method reference in Java?

In this case, instead of using a specific object, you can use the name of the class. Therefore, the first parameter of the functional interface matches the invoking object. This is why we call it the parameter method reference. Rest parameters match the parameters (if any) that are specified by the method.

How do you write system out Println in method reference?

To make the code clearer, you can turn that lambda expression into a method reference: Consumer<String> c = System. out::println; In a method reference, you place the object (or class) that contains the method before the :: operator and the name of the method after it without arguments.

What is a static method reference in Java?

A static method reference allows us to use a static method as a lambda expression. The static methods can be defined in a class, an interface, or an enum.

How do you refer to a method in Java?

Method references are simple, easy-to-read lambda expressions to call/refer and the existing method by name in a lambda expression. You can refer to a static method defined in the class using method references. The following Java example references a static method in Java.

How to use static method in lambda expression?

Static Method References. A static method reference allows us to use a static method as a lambda expression. The static methods can be defined in a class, an interface, or an enum. The following code defines two lambda expressions. The first lambda expression func1 is created by defining an input parameter x and providing lambda expression body ...

How to refer non-static methods?

like static methods, you can refer instance methods also. In the following example, we are describing the process of referring the instance method. In the following example, we are referring non-static methods. You can refer methods by class object and anonymous object. System.out.println ("Hello, this is non-static method.");


1 Answers

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.

like image 80
Oliver Charlesworth Avatar answered Sep 17 '22 21:09

Oliver Charlesworth