<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MiniSoda</title>
    <description>The latest articles on DEV Community by MiniSoda (@kdfemi).</description>
    <link>https://dev.to/kdfemi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F248648%2Fb2904c3c-ea53-4433-a67b-dd21e15b75cc.jpg</url>
      <title>DEV Community: MiniSoda</title>
      <link>https://dev.to/kdfemi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kdfemi"/>
    <language>en</language>
    <item>
      <title>How to write a callback function in Java</title>
      <dc:creator>MiniSoda</dc:creator>
      <pubDate>Mon, 27 Jan 2020 12:10:55 +0000</pubDate>
      <link>https://dev.to/kdfemi/how-to-write-a-callback-function-in-java-3bik</link>
      <guid>https://dev.to/kdfemi/how-to-write-a-callback-function-in-java-3bik</guid>
      <description>&lt;p&gt;This is my first article on dev.to and programming in general. I recently developed an app for my company after completion, i was trying to clean up my code and do some refactoring, when i hit a block where i have two similar codes the only difference was calling a different method, me being a bit of newbie in Java but familiar with JavaScript was puzzled on how i can refactor this line into a method pass a method reference as a method parameter, it was a little bit tricky to get but i was able to get it to work using interface. a little snippet can be found below on how i implemented this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MyClass {
    public static void main(String args[]) {
      myMethod(new GenericMethod(){
          public String call() {
              return "One";
            }
      });
       myMethod(new GenericMethod(){
        public String call() {
          return "Two";
        }
        });

        myMethod(() -&amp;gt; {
                return "Three";
        });
    }

    public static void myMethod(GenericMethod g) {
        System.out.println("This method called " + g.call());
    }  
    public interface GenericMethod {
        public String call();
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I created an interface &lt;em&gt;GenericMethod&lt;/em&gt; with just one method &lt;em&gt;call()&lt;/em&gt; then created a method &lt;em&gt;myMethod&lt;/em&gt; with &lt;em&gt;GenericMethod&lt;/em&gt; as a method parameter, I called &lt;em&gt;GenericMethod.call()&lt;/em&gt; inside my method. to call the &lt;em&gt;myMethod&lt;/em&gt; I simply pass a new instance of &lt;em&gt;GenericMethod&lt;/em&gt; and override the &lt;em&gt;call()&lt;/em&gt; method. You can make the code cleaner by using arrow notation instead of the &lt;em&gt;new&lt;/em&gt; keyword as seen in the code above. The code above output the following to the console&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This method called One
This method called Two
This method called Three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>java</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
