<?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: Raghavan alias Saravanan Muthu</title>
    <description>The latest articles on DEV Community by Raghavan alias Saravanan Muthu (@itsraghz).</description>
    <link>https://dev.to/itsraghz</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%2F237956%2Fa094352b-ae3a-400e-a30c-5b0d7e174b75.jpg</url>
      <title>DEV Community: Raghavan alias Saravanan Muthu</title>
      <link>https://dev.to/itsraghz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itsraghz"/>
    <language>en</language>
    <item>
      <title>Interesting Java Puzzle - Instance Initializers</title>
      <dc:creator>Raghavan alias Saravanan Muthu</dc:creator>
      <pubDate>Wed, 09 Sep 2020 16:02:27 +0000</pubDate>
      <link>https://dev.to/itsraghz/interesting-java-puzzle-instance-initializers-5hd3</link>
      <guid>https://dev.to/itsraghz/interesting-java-puzzle-instance-initializers-5hd3</guid>
      <description>&lt;h1&gt;
  
  
  Instance Initializer Test in Java
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Can you guess what would be the output of the below Java code? &lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class InstanceInitializerTest 
{

    public static void main(String[] args) 
    {
        {
            System.out.println("Feathers");
        }
    }

    {
        System.out.println("Snowy");
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Options
&lt;/h2&gt;

&lt;p&gt;The list of possible outputs are :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Snowy
2. Snowy
   Feathers
3. Feathers
4. Feathers
   Snowy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;The right answer is&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;(3) Feathers&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;p&gt;The instance initializers are the code blocks defined between a pair of curly braces - &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;. They dont really matter even if they are present inside a method, and they are actually counted. &lt;/p&gt;

&lt;p&gt;In the above program, when we run the program, the &lt;code&gt;main()&lt;/code&gt; method gets executed and inside which we have an instance initializer, and it gets executed. As that was the only one executable statement inside the &lt;code&gt;main()&lt;/code&gt; method, the program execution finished there. &lt;/p&gt;

&lt;p&gt;You may wonder why the other instance initializer - which prints &lt;code&gt;Snowy&lt;/code&gt; was not executed. As you see, the 2nd instance initializer was not bound to a method, instead it was bound to the class. As we have not initiated the class, the instance initializer at the class level was not invoked. &lt;/p&gt;

&lt;h2&gt;
  
  
  Work for Brain
&lt;/h2&gt;

&lt;p&gt;Try instantiating the class (inside the &lt;code&gt;main()&lt;/code&gt; method - of course) and see the change in the output.&lt;/p&gt;

&lt;p&gt;You would get the output as either &lt;code&gt;(2) Snowy , Feathers&lt;/code&gt; OR the &lt;code&gt;(4) Feathers, Snowy&lt;/code&gt; depending upon where you instantiate the class - before or after the instance initializer inside the &lt;code&gt;main()&lt;/code&gt; method. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember, the order of the initializers matter a lot :)&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>java</category>
      <category>ocaja</category>
      <category>first</category>
      <category>technical</category>
    </item>
  </channel>
</rss>
