<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Method Reference &#8211; MaXEster Technologies  | Technical Blog</title>
	<atom:link href="https://www.maxester.com/blog/tag/method-reference/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.maxester.com/blog</link>
	<description>Tutorials, Examples and Implementation code for Developers Help</description>
	<lastBuildDate>Mon, 17 Feb 2025 09:17:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.22</generator>
	<item>
		<title>Method Reference In Java 8.</title>
		<link>https://www.maxester.com/blog/2020/01/03/method-reference-in-java-8/</link>
		<comments>https://www.maxester.com/blog/2020/01/03/method-reference-in-java-8/#respond</comments>
		<pubDate>Fri, 03 Jan 2020 07:37:19 +0000</pubDate>
		<dc:creator><![CDATA[Birjesh Gupta]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Lambda Expression]]></category>
		<category><![CDATA[Method Reference]]></category>

		<guid isPermaLink="false">https://www.maxester.com/blog/?p=817</guid>
		<description><![CDATA[<p>One of the most welcome changes in Java 8 was the introduction of&#160;Lambda Expression and Method Reference In java 8.. Method references in java are a special type of lambda expressions. because they&#8217;re often used to create simple lambda expressions&#8230;</p>
<p><a href="https://www.maxester.com/blog/2020/01/03/method-reference-in-java-8/" class="btn-more">Read More<span class="arrow-more">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://www.maxester.com/blog/2020/01/03/method-reference-in-java-8/">Method Reference In Java 8.</a> appeared first on <a rel="nofollow" href="https://www.maxester.com/blog">MaXEster Technologies  | Technical Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[
<p> One of the most welcome changes in Java 8 was the introduction of&nbsp;Lambda Expression and Method Reference In java 8..</p>



<p>Method references in java are a special type of lambda expressions. because they&#8217;re often used to create simple lambda expressions by referencing existing methods. </p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<p> 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>



<p>So in this , we are explaining method reference in java concept in detail. </p>



<h2>Types of method reference in java.</h2>



<ol><li> Method Reference to a static method. </li><li> Reference to a instance method. </li><li> Reference to a constructor. </li></ol>



<h3>1. Method Reference to a static method.</h3>



<p><em><strong>syntax :-</strong></em> <br></p>



<pre class="wp-block-preformatted"><strong>ContainingClass::staticMethodName</strong></pre>



<h4>Example:-</h4>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<p> In the below example, we have defined a functional interface and referring a static method to it&#8217;s functional method max(). </p>



<pre class="wp-block-code"><code>interface Maxester{  
    void max();  
}  
public class MethodReference {  
    public static void welcome(){  
        System.out.println("Welcome, this is static method.");  
    }  
    public static void main(String[] args) {  
        // Referring static method  
        Maxester maxester = MethodReference::welcome;  
        // Calling interface method  
        maxester.max();  
    }  
}  </code></pre>



<p><strong>Output:-</strong></p>



<p>Welcome, this is static method.</p>



<h3>2.  Reference to a instance method.</h3>



<p><strong><em>syntax</em>:-</strong></p>



<pre class="wp-block-preformatted"><strong> containingObject::instanceMethodName </strong></pre>



<h4><strong>Example</strong>:-</h4>



<p> In the below example, we are referring non-static methods. we can refer methods by class object and also an anonymous object. </p>



<pre class="wp-block-code"><code>interface Maxester{  
    void max();  
}  
public class InstanceMethodReference {  
    public void welcome(){  
        System.out.println("Welcome, this is instance method.");  
    }  
    public static void main(String[] args) {  
        InstanceMethodReference methodReference = new InstanceMethodReference(); // Creating object  
        // Referring non-static method using reference  
            Maxester maxester = methodReference::welcome;  
        // Calling interface method  
            Maxester.max();  
            // Referring non-static method using anonymous object  
            Maxester maxester_first = new InstanceMethodReference()::welcome; // You can use anonymous object also  
            // Calling interface method  
            maxester_first.max();  
    }  
}  
</code></pre>



<p><strong>Output</strong>:-</p>



<p>Welcome, this is instance method.</p>



<p>Welcome, this is instance method. </p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<h3>3. Reference to a constructor.</h3>



<p><strong><em>syntax</em></strong>:-</p>



<pre class="wp-block-preformatted"><strong> ClassName::new</strong></pre>



<h4>Example:-</h4>



<pre class="wp-block-code"><code>Interface Maxester{
      NewMaxester max(String value);
}
class NewMaxester{
      NewMaxester(String value){
         System.out.prinln(value);
     }
}
public class ConstrutorRefrenceMethod{
      public static void main(String[] args){
             Maxester maxester = NewMaxester::new;
             maxester.max("Welcome to maxester constructor");
        }
}</code></pre>



<p><strong><em>output</em></strong>:-</p>



<p> Welcome to maxester constructor. </p>



<p><strong><em>External Link:-</em></strong></p>



<p>You can also read much more about Method Reference from below link.</p>



<p><a href="https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html">https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html</a></p>



<p>So i hope, This will be helpful for you.</p>



<p>ThankYou.</p>



<p></p>
<p>The post <a rel="nofollow" href="https://www.maxester.com/blog/2020/01/03/method-reference-in-java-8/">Method Reference In Java 8.</a> appeared first on <a rel="nofollow" href="https://www.maxester.com/blog">MaXEster Technologies  | Technical Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.maxester.com/blog/2020/01/03/method-reference-in-java-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
