How can I store a method in a variable in Java 8?

前端 未结 4 1700
我寻月下人不归
我寻月下人不归 2020-12-03 06:28

Is it possible to store a method into a variable? Something like

 public void store() {
     SomeClass foo = ;
     //...
     String          


        
4条回答
  •  天涯浪人
    2020-12-03 07:27

    You can use method reference like -

    System.out::println  
    

    Which is equivalent to the lambda expression -

    x -> System.out.println(x).  
    

    Moreover you can user reflection to store method and it will works for the earlier version of java too.

提交回复
热议问题