{"id":3115,"date":"2019-02-28T16:34:38","date_gmt":"2019-02-28T16:34:38","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3115"},"modified":"2019-02-28T16:59:54","modified_gmt":"2019-02-28T16:59:54","slug":"method-references-in-java","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/method-references-in-java\/","title":{"rendered":"Method References in Java"},"content":{"rendered":"<p>Method reference is used to refer method of functional interface. It is compact and easy form of lambda expression. Each time when you are using lambda expression to just referring a method, you can replace your lambda expression with method reference.<\/p>\n<p>3 types of method references:<\/p>\n<ol>\n<li>Reference to a static method<\/li>\n<li>Reference to an instance method<\/li>\n<li>Reference to a constructor<\/li>\n<\/ol>\n<p><strong>Reference to a static method<\/strong><br \/>\n<strong>Syntax<\/strong><\/p>\n<pre>\r\n  ClassName::MethodName\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.function.BiFunction;\r\nclass Arithmetic \r\n {\r\n public static int add(int a, int b) \r\n {\r\n  return a + b;\r\n }\r\n}\r\npublic class MethodReference \r\n{\r\n public static void main(String&#x5B;] args) \r\n {\r\n  BiFunction &lt; Integer, Integer, Integer &gt; adder = Arithmetic::add;\r\n  int result = adder.apply(10, 20);\r\n  System.out.println(result);\r\n }\r\n}\r\n<\/pre>\n<p><strong>Reference to an instance method<\/strong><br \/>\n<strong>Syntax<\/strong><\/p>\n<pre>Object::methodName<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.function.BiFunction;\r\nclass Arithmetic \r\n {\r\n public int add(int a, int b) \r\n {\r\n  return a + b;\r\n }\r\n}\r\npublic class MethodReference \r\n{\r\n public static void main(String&#x5B;] args) \r\n {\r\n  Arithmetic objArithmetic = new Arithmetic();\r\n  BiFunction &lt; Integer, Integer, Integer &gt; adder = objArithmetic::add;\r\n  int result = adder.apply(10, 20);\r\n  System.out.println(result);\r\n }\r\n}\r\n<\/pre>\n<p><strong>Reference to a constructor<\/strong><br \/>\n<strong>Syntax<\/strong><\/p>\n<pre>\r\nClassName::new  \r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ninterface Messageable {\r\n Message getMessage(String msg);\r\n}\r\nclass Message {\r\n Message(String msg) {\r\n  System.out.print(msg);\r\n }\r\n}\r\npublic class ConstructorReference {\r\n public static void main(String&#x5B;] args) {\r\n  Messageable hello = Message::new;\r\n  hello.getMessage(&quot;Hello&quot;);\r\n }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Method reference is used to refer method of functional interface. It is compact and easy form of lambda expression. Each time when you are using lambda expression to just referring a method, you can replace your lambda expression with method reference. 3 types of method references: Reference to a static method Reference to an instance&hellip; <a href=\"https:\/\/codethataint.com\/blog\/method-references-in-java\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[250],"tags":[253],"class_list":["post-3115","post","type-post","status-publish","format-standard","hentry","category-lambdas","tag-interview"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3115"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3115\/revisions"}],"predecessor-version":[{"id":3116,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3115\/revisions\/3116"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}