<?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; programming</title>
	<atom:link href="http://code.deepinspace.net/category/programming/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>A Ruby script to search bookstores online</title>
		<link>http://code.deepinspace.net/2009/12/26/a-ruby-script-to-search-bookstores-online/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-ruby-script-to-search-bookstores-online</link>
		<comments>http://code.deepinspace.net/2009/12/26/a-ruby-script-to-search-bookstores-online/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 12:11:09 +0000</pubDate>
		<dc:creator>hrish</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[projects]]></category>

		<guid isPermaLink="false">http://code.deepinspace.net/?p=72</guid>
		<description><![CDATA[I started dabbling in Ruby some weeks back. The initial interest was sparked after reading &#8220;Treating Code as an Essay&#8221; (Yukihiro Matsumoto) &#8211; one of the chapters in Beautiful Code. So I started doing these bootstrapping exercises in Ruby. Some of the exercises are good &#8211; but nothing beats doing a small project to learn [...]]]></description>
			<content:encoded><![CDATA[<p>I started dabbling in Ruby some weeks back. The initial interest was sparked after reading &#8220;Treating Code as an Essay&#8221; (Yukihiro Matsumoto) &#8211; one of the chapters in <a title="Beautiful Code" href="http://www.amazon.com/Beautiful-Code-Leading-Programmers-Practice/dp/0596510047" target="_blank">Beautiful Code</a>. So I started doing these <a id="tgx1" title="www.knowing.net" href="http://www.knowing.net/index.php/2006/06/16/15-exercises-to-know-a-programming-language-part-1/" target="_blank">bootstrapping</a> exercises in Ruby. Some of the exercises are good &#8211; but nothing beats doing a small project to learn a new language.</p>
<p>I buy a lot of books, mostly online. There are a few good online bookstores in India, notably Flipkart.com, Infibeam.com and Indiaplaza.in (Sadly, Amazon does not have full-fledged shipping to India yet). The way I usually search for a book in online bookstores is (was, till now)</p>
<ol>
<li>Go to books.google.com and enter the book title</li>
<li>Click on the best match</li>
<li>Click on &#8216;All Sellers&#8217; on the left of the page</li>
<li>The Indian bookstores are usually listed towards the bottom. It does not include all stores, and sometimes the prices are not listed. I have to go to each individual site and check them out.</li>
</ol>
<p>I wanted to collapse these steps into one &#8211; a simple script that would accept the name of the book and show results from all these bookstores, with comparative pricing. And the result was this</p>
<p><a id="cb6." title="http://github.com/talonx/book-search" href="http://github.com/talonx/book-search" target="_blank">http://github.com/talonx/book-search</a></p>
<p>It&#8217;s in Ruby, runs from the command line and writes the output to an HTML in the same directory called &#8216;search.html&#8217;. Much needs to be done, like</p>
<ul>
<li>Price based listing with the lowest on top</li>
<li>A web interface for the search</li>
<li>Add more bookstores &#8211; it&#8217;s only Flipkart.com, Infibeam.com, Indiaplaza and Bookadda.com right now.</li>
</ul>
<p>To run the script, type this (you need Ruby 1.8.x, available from <a title="http://www.ruby-lang.org/en/downloads/" href="http://www.ruby-lang.org/en/downloads/" target="_blank">http://www.ruby-lang.org/en/downloads/</a> and the Hpricot HTML parser library, available from <a title="http://github.com/whymirror/hpricot" href="http://github.com/whymirror/hpricot" target="_blank">http://github.com/whymirror/hpricot</a>)</p>
<blockquote><p>ruby lib\book-search.rb &#8220;&lt;book title (in quotes if it has spaces)&gt;&#8221;</p></blockquote>
<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%2F12%2F26%2Fa-ruby-script-to-search-bookstores-online%2F', 'A+Ruby+script+to+search+bookstores+online')" onclick="STTAFFUNC.cw(this, {id:'2008071112-2', link: 'http%3A%2F%2Fcode.deepinspace.net%2F2009%2F12%2F26%2Fa-ruby-script-to-search-bookstores-online%2F', title: 'A+Ruby+script+to+search+bookstores+online' });"/></a>]]></content:encoded>
			<wfw:commentRss>http://code.deepinspace.net/2009/12/26/a-ruby-script-to-search-bookstores-online/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
