<?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>Hrishikesh Barua &#124; code@deepinspace &#187; java</title>
	<atom:link href="http://code.deepinspace.net/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.deepinspace.net</link>
	<description></description>
	<lastBuildDate>Mon, 24 May 2010 06:50:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Instance Initializers in Java</title>
		<link>http://code.deepinspace.net/2010/05/22/instance-initializers-in-java/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=instance-initializers-in-java</link>
		<comments>http://code.deepinspace.net/2010/05/22/instance-initializers-in-java/#comments</comments>
		<pubDate>Sat, 22 May 2010 16:27:11 +0000</pubDate>
		<dc:creator>hrish</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://code.deepinspace.net/?p=101</guid>
		<description><![CDATA[Take a look at this simple code Code Snippet 1 public class Init { { System.out.println(&#34;In the beginning was the command line&#34;); } public Init() { System.out.println(&#38;quot;Created an instance&#38;quot;); } public static void main(String[] args) { Init init = new Init(); } } What do you think the output is? It&#8217;s this - In the [...]]]></description>
			<content:encoded><![CDATA[<p>Take a look at this simple code</p>
<p><span style="color: #0000ff;">Code Snippet 1</span></p>
<pre class="brush: java;">
public class Init {
    {
        System.out.println(&quot;In the beginning was the command line&quot;);
    }

    public Init()
    {
        System.out.println(&amp;quot;Created an instance&amp;quot;);
    }

    public static void main(String[] args)
    {
        Init init = new Init();
    }
}
</pre>
<p>What do you think the output is? It&#8217;s this -</p>
<pre>    In the beginning was the command line
    Created an instance
</pre>
<p>The &#8216;hanging&#8217; braces at the start of the class definition are instance initializers. Most of us are more familiar with static initializers -</p>
<p><span style="color: #0000ff;">Code Snippet 2</span></p>
<pre class="brush: java;">
static
{
    //Do stuff
}
</pre>
<p>Instance initializers (II) are not seen often in everyday Java code &#8211; so they might seem odd at first. They are executed every time an instance of that class is created, before the statements in the constructor are executed. (See <em>The Java Language Specification 3 </em>section 8.6).</p>
<p>One use of IIs can be to execute something whenever an instance is created, and the class has multiple constructors, without calling it in every single constructor.<br />
Another one which has become popular is populating collections during declaration, in the style of Ruby or Python single-line initializers -</p>
<p><span style="color: #0000ff;">Code Snippet 3</span></p>
<pre class="brush: java;">
private Set&lt;String&gt; names = new HashSet&lt;String&gt;() {
    {
        add(&quot;Rigel&quot;);
        add(&quot;Vega&quot;);
        add(&quot;Antares&quot;);
    }
};
</pre>
<p>This idiom was how I encountered IIs first while reading somebody&#8217;s blog.<br />
What is actually happening here?</p>
<ol>
<li>An anonymous inner class is created.</li>
<li>An instance initializer block is added to the anon inner class.</li>
<li>Objects are added to the instance of that class when the names variable is initialized.</li>
</ol>
<p>Now take this scenario<br />
<span style="color: #0000ff;">Code Snippet 4</span></p>
<pre class="brush: java;">
public class WrongUsage {

    private Set&lt;String&gt; names;

    {
        add(&quot;pleiades&quot;);
    }

    public void WrongUsage()
    {
        names = new HashSet&lt;String&gt;();
    }

    public void add(String name)
    {
        names.add(name);
    }
}
</pre>
<p>Based on what we have seen above, the names set is used before it&#8217;s initialized. So this throws a NullPointerException.<br />
Let&#8217;s take another case &#8211; similar to the above but involving inheritance.</p>
<p><span style="color: #0000ff;">Code Snippet 5</span></p>
<pre class="brush: java;">
public class MyHashSet extends HashSet {
    {
        add(&quot;pleiades&quot;);
        System.out.println(&quot;Added&quot;);
    }

    public MyHashSet()
    {
        super();
        System.out.println(&quot;After calling super&quot;);
    }

    public static void main(String[] args)
    {
        Set set = new MyHashSet();
    }
}
</pre>
<p>This runs, with the output being</p>
<pre>    Added
    After calling super
</pre>
<p>In this case, add() internally uses the inner HashMap inside HashSet which is initialized in the HashSet constructor. This implies that the instance initializer is invoked before the class constructor, but after the superclass constructor (The super call is redundant here. It will be called anyway).</p>
<p>So the sequence is</p>
<ol>
<li> Superclass initialization (this includes superclass instance initializers and constructor)</li>
<li> Current class&#8217;s Instance initializers</li>
<li> Current class&#8217;s Constructor</li>
</ol>
<p>This is why the code in Code Snippet 3 does not throw an NPE &#8211; because it&#8217;s a case of inheritance (the anon inner class is a subclass of HashSet)</p>
<script type="text/javascript" src="http://cdn.socialtwist.com/2008071112-2/script.js"></script><a class="st-taf" href="http://tellafriend.socialtwist.com" onclick="return false;" style="border:0;padding:0;margin:0;"><img alt="SocialTwist Tell-a-Friend" style="border:0;padding:0;margin:0;" src="http://images.socialtwist.com/2008071112-2/button.png"onmouseout="STTAFFUNC.hideHoverMap(this)" onmouseover="STTAFFUNC.showHoverMap(this, '2008071112-2', 'http%3A%2F%2Fcode.deepinspace.net%2F2010%2F05%2F22%2Finstance-initializers-in-java%2F', 'Instance+Initializers+in+Java')" onclick="STTAFFUNC.cw(this, {id:'2008071112-2', link: 'http%3A%2F%2Fcode.deepinspace.net%2F2010%2F05%2F22%2Finstance-initializers-in-java%2F', title: 'Instance+Initializers+in+Java' });"/></a>]]></content:encoded>
			<wfw:commentRss>http://code.deepinspace.net/2010/05/22/instance-initializers-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wondering about the state of Java Developers</title>
		<link>http://code.deepinspace.net/2009/08/29/wondering-about-the-state-of-java-developers/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=wondering-about-the-state-of-java-developers</link>
		<comments>http://code.deepinspace.net/2009/08/29/wondering-about-the-state-of-java-developers/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 17:41:15 +0000</pubDate>
		<dc:creator>hrish</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[interview]]></category>

		<guid isPermaLink="false">http://code.deepinspace.net/?p=38</guid>
		<description><![CDATA[A friend of mine forwarded this article by Yakov Fain on sys-con.com - http://in.sys-con.com/node/1040135 The essence of the article is this The author interviewed a lot of people for developer positions, and most of them who call themselves Java developers and cite extensive experience in J2EE lack basic knowledge of core Java. This might sound [...]]]></description>
			<content:encoded><![CDATA[<p>A friend of mine forwarded this article by Yakov Fain on sys-con.com -</p>
<p><a title="http://in.sys-con.com/node/1040135" href="http://in.sys-con.com/node/1040135" target="_blank">http://in.sys-con.com/node/1040135</a></p>
<p>The essence of the article is this</p>
<p><em>The author interviewed a lot of people for developer positions, and most of them who call themselves Java developers and cite extensive experience in J2EE lack basic knowledge of core Java</em>.</p>
<p>This might sound suspiciously like a gross generalization, but I believe that&#8217;s not the case. I had a similar experience when I interviewed people for developer positions on my team last month. The position called for both Java and Javascript experience. These are the things I encountered -</p>
<ul>
<li>Most people who have worked solely on services (read outsourced) projects list all J* technologies on their resume, but know very little in depth of Java programming.</li>
<li>There are people who lack any kind of programmer mentality or skills at all and put their current role as something like Programmer Analyst, and this fact cannot be ascertained from their resume alone. They often try to highlight other (non-software development) achievements.</li>
<li>SCJP certification is no guarantee that a person can code in Java (Surprise? Not at all)</li>
<li>There are people who have 3.5 years of experience, with multiple services projects under their belts, and familiarity with a host of technologies, who cannot write a Java class which will print out the prime numbers between 0 and 100.</li>
<li>Most core CS concepts are forgotten after 2-3 years of working in services projects.</li>
</ul>
<p>Please note that I am not generalizing, but these facts do indicate a problem somewhere. These developers actually a represent a very small distinct sample of the worldwide developer community, since all my interviews were done in India (both face to face in my Hyderabad office and over the phone).</p>
<p>Another interesting point I noted was that most non-Javascript developers think that Javascript is used only for form validation. Such usage also qualifies as &#8216;extensive Javascript knowledge&#8217; in their resumes. <br/><br />
What should I conclude from this? Is this malaise widespread in other parts of the world as well? Is it specific to developers in India working on outsourced projects? (No, as the link by Yakov Fain shows) Is it a result of outsourcing, leading to a lack of innovation? Or is the innovation there, but the signal to noise ratio too low?<br/><br/></p>
<script type="text/javascript" src="http://cdn.socialtwist.com/2008071112-2/script.js"></script><a class="st-taf" href="http://tellafriend.socialtwist.com" onclick="return false;" style="border:0;padding:0;margin:0;"><img alt="SocialTwist Tell-a-Friend" style="border:0;padding:0;margin:0;" src="http://images.socialtwist.com/2008071112-2/button.png"onmouseout="STTAFFUNC.hideHoverMap(this)" onmouseover="STTAFFUNC.showHoverMap(this, '2008071112-2', 'http%3A%2F%2Fcode.deepinspace.net%2F2009%2F08%2F29%2Fwondering-about-the-state-of-java-developers%2F', 'Wondering+about+the+state+of+Java+Developers')" onclick="STTAFFUNC.cw(this, {id:'2008071112-2', link: 'http%3A%2F%2Fcode.deepinspace.net%2F2009%2F08%2F29%2Fwondering-about-the-state-of-java-developers%2F', title: 'Wondering+about+the+state+of+Java+Developers' });"/></a>]]></content:encoded>
			<wfw:commentRss>http://code.deepinspace.net/2009/08/29/wondering-about-the-state-of-java-developers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
