<?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>Techtracer &#187; Java</title>
	<atom:link href="http://techtracer.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://techtracer.com</link>
	<description>Tracing the course of Web Technology</description>
	<lastBuildDate>Tue, 06 Jan 2009 12:14:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
  <link>http://techtracer.com</link>
  <url>http://techtracer.com/wp-includes/images/favicon.png</url>
  <title>Techtracer</title>
</image>
		<item>
		<title>Reflection in Java &#8211; Simplified</title>
		<link>http://techtracer.com/2008/11/24/reflection-in-java-simplified/</link>
		<comments>http://techtracer.com/2008/11/24/reflection-in-java-simplified/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 04:23:37 +0000</pubDate>
		<dc:creator>nitinpai</dc:creator>
				<category><![CDATA[Concepts]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Reflection]]></category>
		<category><![CDATA[Java Reflection tutorial]]></category>

		<guid isPermaLink="false">http://techtracer.com/?p=276</guid>
		<description><![CDATA[There are times when you wish you had some access to the information present in your Java classes. Information, such as which fields, methods and constructors are defined in it. This information is called as the metadata of a class and there is a way in which you can access easily without doing tedious coding [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when you wish you had some access to the information present in your Java classes. Information, such as which fields, methods and constructors are defined in it. This information is called as the <strong>metadata</strong> of a class and there is a way in which you can access easily without doing tedious coding or resorting to any hacks. It is called as <strong>Reflection</strong>.</p>
<p><strong>Reflection in Java</strong> is done by using using the <strong>java.lang.Class</strong> class. This class provides access to all the information within your own applications classes or even the regular java library classes. Let’s say you have a class in a package called <em>com.tt.company.Employee </em>then if you need the metadata of the Employee class you would have to first assign it to the Class as :</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">Class</span> c = <span style="color: #003399; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span>“com.<span style="color: #006633;">tt</span>.<span style="color: #006633;">company</span>.<span style="color: #006633;">Employee</span>”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><span style="text-decoration: underline;"><strong>Accessing Fields, Methods and Constructors with Reflection</strong></span></p>
<p>If you are not sure which fields and methods are declared in a class and you don’t have access to the source code then you can easily get this information by simple looping.</p>
<p>For example, if I wanted to access the fields and methods declared in the class <em>Employe</em>e then its as simple as the below code.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">Field</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> fields = c.<span style="color: #006633;">getFields</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">Method</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> methods = c.<span style="color: #006633;">getMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Field</span> f : fields<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Field: &quot;</span> + f.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Method</span> m : methods<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Method:&quot;</span> + m.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You might get some additional methods if you use the <strong>getMethods() </strong>method. This is because, the <strong>getMethods() </strong>definition also chooses those methods which are present in any of the super classes present. In this case, if <em>Employee</em> class does not have any super class, but still it is a subclass of <em>Objec</em>t. Hence methods defined in the <em>Object</em> class would also form a part of the output.</p>
<p>In order to avoid the methods defined in the super class you should use the <strong>getDeclaredMethods()</strong> instead.</p>
<p><strong><span style="text-decoration: underline;">Note:</span></strong> An important point worth mentioning over here is that, the loop would produce only those methods or fields which are marked public. If you don’t have a public field or method it won’t be considered. In order to loop over them, use the <strong>getDelcaredFields() </strong>or <strong>getDeclaredMethods()</strong></p>
<p><span style="text-decoration: underline;"><strong>Fun Things to do with Relection in Java</strong></span></p>
<p>You can discover new techniques of programming once you feel the power of Reflection in your hands. The new techniques would include instantiation of objects dynamically, setting of values to fields and invoking methods without calling them explicitly.</p>
<p>Let’s take the case of a <strong>Singleton </strong>class into consideration for showing the true power of Reflection. For a Java novice, a singleton class is the one which has a<strong> private constructor</strong>. It isn’t practically true, but let’s assume that it is. Using reflection, this concept of singleton can be broken.</p>
<p>Here is an example of a Singleton class:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SingleCandidate <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">int</span> counter = <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> SingleCandidate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
counter++<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Instance #&quot;</span> + counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> showName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A Singleton pattern dictates that only one instance of the object must be present at a given point of time in a JVM. So now, let’s take the above Singleton class for a ride with Reflection:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">Class</span> singleton = SingleCandidate.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">Constructor</span> privateConstructor =  singleton.<span style="color: #006633;">getDeclaredConstructors</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
privateConstructor.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//This is scary</span>
&nbsp;
SingleCandidate obj = <span style="color: #009900;">&#40;</span>SingleCandidate<span style="color: #009900;">&#41;</span> privateConstructor.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
SingleCandidate obj2 = <span style="color: #009900;">&#40;</span>SingleCandidate<span style="color: #009900;">&#41;</span> privateConstructor.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code is self explanatory and you will realize that in this way you can easily create many instances of the possible candidate for a singleton class in the JVM. How fool proof was that?</p>
<p><span style="text-decoration: underline;"><strong>Setting Values to Private Fields!</strong></span></p>
<p>What did you learn in encapsulation? Was it making fields private and public so that private fields could be accessed only by the class in which they are declared?  Reflection can break this too.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Even more scary</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">Field</span> privateField = singleton.<span style="color: #006633;">getDeclaredField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
privateField.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
privateField.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span>obj, <span style="color: #0000ff;">&quot;You are not a singleton anymore&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Access private Constructor</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Accessing private Constructor: &quot;</span> + privateConstructor.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//Get a private field value</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Accessing private Field: &quot;</span> + privateField.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Invoke a method</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">Method</span> method = singleton.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;showName&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><span style="text-decoration: underline;"><strong>Reflection with Generics</strong></span></p>
<p>If you have tried out the above code in Eclipse you might see some warnings for the Class or Constructor usage. The warning message will be:</p>
<blockquote><p>Class is a raw type. References to generic type Class&lt;T&gt; should be parameterized</p>
<p>Constructor is a raw type. References to generic type Constructor&lt;T&gt; should be parameterized</p></blockquote>
<p>To suppress these warnings you will have to use the generic parameter &#8220;?&#8221; as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">Class<span style="color: #339933;">&lt;?&gt;</span> singleton = SingleCandidate.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
&nbsp;
Constructor<span style="color: #339933;">&lt;?&gt;</span> privateConstructor =  singleton.<span style="color: #006633;">getDeclaredConstructors</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The questions mark symbol is a <a href="http://java.sun.com/docs/books/tutorial/extra/generics/wildcards.html" target="_blank">wildcard type</a> used in generics. This only applies only if you using <strong>Java 5</strong> or higher versions.</p>
<p><span style="text-decoration: underline;"><strong>Uses of reflection</strong></span></p>
<p>Reflection is <strong>powerful technique</strong> when used appropriately. The major usage of reflection comes in when you are building your own framework, compilers or development tools in which you need access to the metadata of the classes created by end users. You surely might have already used the best products of Reflection without even knowing about it.</p>
<ul>
<li>JSP gets converted to Servlets using Reflection.</li>
<li>The <strong>Spring</strong> framework uses Reflection to administer the references which you declare in your s<em>pring-config.xml</em> without you explicitly instantiating any object. The whole concept of <a href="http://techtracer.com/2007/04/26/getting-started-with-ioc-a-simplified-tutorial/" target="_self">Dependency Injection (Inversion of Control)</a> is conveniently implemented with Reflection.</li>
<li>Reflection also gave birth to the concept of <strong>aspect oriented programming</strong>.</li>
</ul>
<p>Reflection is not limited just to this post and nor Reflection is used for the purpose of breaking security. This article was aimed at showing you a direction with which you can explore Reflection more and discover new principles and techniques to build better applications.</p>
<h2>Related posts:</h2><ul><li><a href="http://techtracer.com/2007/03/28/convert-date-to-string-and-string-to-date-in-java/" rel="bookmark" title="Permanent Link: Convert Date to String and String to Date in Java">Convert Date to String and String to Date in Java</a></li><li><a href="http://techtracer.com/2007/04/26/getting-started-with-ioc-a-simplified-tutorial/" rel="bookmark" title="Permanent Link: Getting started with IoC &#8211; A simplified tutorial">Getting started with IoC &#8211; A simplified tutorial</a></li><li><a href="http://techtracer.com/2007/04/04/java-5-generics-changing-for-good/" rel="bookmark" title="Permanent Link: Java 5 Generics &#8211; Changing for good">Java 5 Generics &#8211; Changing for good</a></li><li><a href="http://techtracer.com/2006/10/15/microsoft-internet-explorer-7-nothing-special/" rel="bookmark" title="Permanent Link: Microsoft Internet Explorer 7 &#8211; Nothing Special">Microsoft Internet Explorer 7 &#8211; Nothing Special</a></li><li><a href="http://techtracer.com/2007/08/01/j2ee-or-jee-java-5-or-java-15-is-sun-crazy/" rel="bookmark" title="Permanent Link: J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?">J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?</a></li></ul><br /><a href="http://techtracer.com/">Techtracer.com</a> Copyright &copy; 2008<br /> ]]></content:encoded>
			<wfw:commentRss>http://techtracer.com/2008/11/24/reflection-in-java-simplified/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Mystery of Accessibility in Local Inner Classes</title>
		<link>http://techtracer.com/2008/04/14/mystery-of-accessibility-in-local-inner-classes/</link>
		<comments>http://techtracer.com/2008/04/14/mystery-of-accessibility-in-local-inner-classes/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 16:33:35 +0000</pubDate>
		<dc:creator>nitinpai</dc:creator>
				<category><![CDATA[Concepts]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://techtracer.com/2008/04/14/mystery-of-accessibility-in-local-inner-classes/</guid>
		<description><![CDATA[Here is an interesting concept which I came across just recently and considered it worth sharing. I was reading about inner classes concepts where in, there is a type of inner class called the local inner class. Local inner classes are those classes which reside within the function of a method belonging to an outer [...]]]></description>
			<content:encoded><![CDATA[<p>Here is an interesting concept which I came across just recently and considered it worth sharing. I was reading about inner classes concepts where in, there is a type of inner class called the<strong> local inner class</strong>. Local inner classes are those classes which reside <strong>within</strong> the function of a <strong>method</strong> belonging to an outer class. The code can be something shown like this.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LocalInnerClassTest <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> defineInnerClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">class</span> MyLocalInnerClass <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>				
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now lets suppose we want to pass a variable in the <strong>defineInnerClass()</strong> and pass it to the <em><strong>doSomething()</strong></em> for some computation, then according to the specifications on local inner classes methods we must declare the variables as <strong>final</strong> or else it will result in a compile time error. So the resulting code must be something as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LocalInnerClassTest <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> defineInnerClass<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #006600; font-weight: bold;">int</span> var<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">class</span> MyLocalInnerClass <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>var<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>where<em><strong> var </strong></em>is the variable that must be declared as <strong>final</strong> to be passed into the <em><strong>doSomething()</strong></em> method of <em><strong>MyLocalInnerClass</strong></em>. Now the mysterious question which I faced was why exactly such a specification has been outlined. Why can&#8217;t the inner class simply take the variable as it is and process further.</p>
<p>In order to understand this reason as to why local inner classes can access only final variables, we have to learn how exactly inner classes get translated to the <strong>byte code</strong>. The moment you come to know this, you can easily see the logic behind making the variables final.</p>
<p><strong>How Are Inner Classes Translated to the Byte Code</strong></p>
<p>Here is the secret. Inner classes as you must have known them from a long time are still mysterious to the  <strong>JVM</strong>. Yes its true. Inner classes have been implemented only to the <strong>compiler</strong> level. When the classes are compiled which contain inner classes, the byte code which gets generated does not actually implement inner classes as a class within a class. The book on <a href="http://www.amazon.com/Core-Java-1-1-1-Fundamentals/dp/0137669577" target="_blank">Core Java</a> from makes this statement:</p>
<blockquote><p>&#8220;Inner classes are translated into regular class files with $ (dollar signs) delimiting outer and inner class names and the virtual machine does not have any special knowledge about them&#8221;</p></blockquote>
<p>That means when the above class file from the example is compiled it will generate two class files such as:</p>
<ol>
<li>LocalInnerClassTest.class</li>
<li>LocalInnerClassTest$MyLocalInnerClass.class</li>
</ol>
<p><strong>Unfurling the mystery of final variables</strong></p>
<p>If you apply logic to the above theory of inner class at the byte code level, you have the answer to the mystery of having final variables. For explanation purpose, lets take the same example.</p>
<p>First lets say we make a call to the <em><strong>defineInnerClass()</strong></em> by creating an instance of <em><strong>LocalInnerClassTest</strong></em>. At this point, the instance of <em><strong>MyLocalInnerClass</strong></em> is still not present because the JVM treats it as a separate class at the byte code level. So when the call to <em><strong>defineInnerClass()</strong></em> is made the JVM tries to instantiate an object of <em><strong>MyLocalInnerClass</strong></em>.</p>
<p>But here we run into a problem. The function <em><strong>doSomething()</strong></em> accesses the <em><strong>var</strong></em> variable which is passed down from the outer class method. If you can simply apply logic over here, you can see the problem. How should the JVM pass the variable which has been declared in one class file to the method in another class file?</p>
<p>In order to solve this big problem, the JVM acts smart. It makes a requirement for the developer to make the variable passed from the method of an outer class to be declared as <strong>final</strong>. How would this solve the problem, you may ask? When you declare the variable <em><strong>var </strong></em>as final the compiler does a trick. The trick being, it quietly places a hidden variable with the name <em><strong>val$var</strong></em> inside the 2nd compiled class file.</p>
<p>The variable <em><strong>val$var</strong></em> is assigned the same value which has been assigned to <strong>var</strong> since now the compiler knows that the value cannot be changed as it has been declared <strong>final</strong>. This is very clever, since final variable must always be assigned before compilation.</p>
<p>So there you go. Now, when you run the program from within an application the inner class already has the value which has been assigned to <em><strong>var</strong></em> through its inner hidden variable <em><strong>val$var</strong></em> and thus the mystery gets solved. Therefore you have the concept that local inner class methods can have access to only the final variables of the outer class.</p>
<p><strong>Hey, Local Inner Classes can even access Outer Class member variables directly. How?<br />
</strong></p>
<p>Ok, now that we have tackled the above mystery, this is more simpler. The below code gets perfectly compiled</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LocalInnerClassTest <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #006600; font-weight: bold;">int</span> var2<span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> defineInnerClass<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #006600; font-weight: bold;">int</span> var<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 	  <span style="color: #000000; font-weight: bold;">class</span> MyLocalInnerClass<span style="color: #009900;">&#123;</span>
 		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 			<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>var+var2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 		<span style="color: #009900;">&#125;</span>
     	  <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can notice that the local inner class has direct access to even the private variable <em><strong>var2</strong></em> of its outer class. The reason behind the accessibility of outer member variables directly inside the local inner classes is that, once again the compiler cleverly places one more hidden variable named as <em><strong>this$0</strong></em> which is a <strong>final</strong> instance variable of the <strong>outer class type</strong>. When the inner class object is instantiated the variable <em><strong>this$0</strong></em> is given a reference to the outer variable with<strong> direct access  privileges</strong>. Hence  you can access the outer class  member variables directly from within the inner class.  Smart isn&#8217;t it?</p>
<h2>Related posts:</h2><ul><li><a href="http://techtracer.com/2007/10/14/unleash-the-power-in-your-blog-with-google-custom-search/" rel="bookmark" title="Permanent Link: Unleash the Power in Your Blog with Google Custom Search">Unleash the Power in Your Blog with Google Custom Search</a></li><li><a href="http://techtracer.com/2007/10/13/hp-snapfish-india-for-cost-effective-printing/" rel="bookmark" title="Permanent Link: HP Snapfish India, For Cost Effective Printing">HP Snapfish India, For Cost Effective Printing</a></li><li><a href="http://techtracer.com/2007/06/21/learn-the-servlet-api-a-step-by-step-approach/" rel="bookmark" title="Permanent Link: Learn the Servlet API &#8211; A step by step approach">Learn the Servlet API &#8211; A step by step approach</a></li><li><a href="http://techtracer.com/2007/04/07/annotations-in-pojo-a-boon-or-a-curse/" rel="bookmark" title="Permanent Link: Annotations in POJO &#8211; a boon or a curse?">Annotations in POJO &#8211; a boon or a curse?</a></li><li><a href="http://techtracer.com/2007/04/26/getting-started-with-ioc-a-simplified-tutorial/" rel="bookmark" title="Permanent Link: Getting started with IoC &#8211; A simplified tutorial">Getting started with IoC &#8211; A simplified tutorial</a></li></ul><br /><a href="http://techtracer.com/">Techtracer.com</a> Copyright &copy; 2008<br /> ]]></content:encoded>
			<wfw:commentRss>http://techtracer.com/2008/04/14/mystery-of-accessibility-in-local-inner-classes/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Resolving the &#8220;Unsupported major.minor version 49.0&#8243; Error</title>
		<link>http://techtracer.com/2007/10/10/resolving-the-unsupported-majorminor-version-490-error/</link>
		<comments>http://techtracer.com/2007/10/10/resolving-the-unsupported-majorminor-version-490-error/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 15:38:13 +0000</pubDate>
		<dc:creator>nitinpai</dc:creator>
				<category><![CDATA[Issues]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java EE]]></category>

		<guid isPermaLink="false">http://techtracer.com/2007/10/10/resolving-the-unsupported-majorminor-version-490-error/</guid>
		<description><![CDATA[Many a times I have come across my friends who get this error and are clueless because this error is not something related to the programming logic neither to the server which shows this error either on accessing a servlet or a service. You have to know that this error comes when a class compiled [...]]]></description>
			<content:encoded><![CDATA[<p>Many a times I have come across my friends who get this error and are clueless because this error is not something related to the programming logic neither to the server which shows this error either on accessing a servlet or a service. You have to know that this error comes when a class compiled on a newer version is being tried to run on an older JVM i.e. If you try to run a JDK 1.5 compiled class in an environment having JRE 1.4 the JRE will give you the error,</p>
<blockquote><p> <strong>Unsupported major.minor version 49.0</strong></p></blockquote>
<p>This error is given because the new features introduced in the newer version of Java are not present in the older version. So you have to be careful for using such class files on older environments. For resolving this error you have to follow some simple tips as follows:</p>
<ol>
<li>Check you java version by running the command <strong>java &#8211; version</strong></li>
<li>Check your environment variable JAVA_HOME and the path associated with it</li>
<li>Check your <strong>server</strong> installation if it was installed on an older version</li>
<li>Check which path exists in the environment entries</li>
</ol>
<p>These are the quick tips you would need to do in order to get rid of the error. Sometimes it may happen that the system which you are using might be loaded with 2 to 3 version of Java and you might be required to work on the newer one. In such cases always there is a <strong>confusion</strong> as to what went wrong and with which version. So if you are left in such crises do the following quick action tips:</p>
<ol>
<li>Edit your <strong>JAVA_HOME</strong> with the latest java home folder path</li>
<li>Edit your <strong>path</strong> variable which might already have been set with older version</li>
<li>For this goto <strong>My Computer</strong> &gt; <strong>Properties</strong> &gt; <strong>Advanced </strong>&gt; <strong>Environment Variables</strong> &gt; <strong>path</strong></li>
<li><strong>Remove</strong> the earlier version path which might be something like <em>&lt;java_installed_path&gt;</em>/bin and<strong> replace</strong> it with newer version path</li>
<li>If your server was installed on a older version of Java and if it not been used then its better to <strong>uninstall</strong> it and then install it after making the above changes.</li>
</ol>
<p><u><strong>Note:</strong></u><strong> </strong>You can check if your server was installed on an older version or not by opening the <strong>startup.bat</strong> of your Tomcat server or any <strong>start up </strong>script of other servers and finding the part where the script itself sets the <strong>JAVA_HOME</strong> variable. If you find that it has been set to a older version then it won&#8217;t do any good just by replacing it with a newer version path. You would ultimately have to reinstall the server as per my understanding.</p>
<p>It would be interesting to know if the same can be resolved without having to reinstall the server. Please suggest some alternatives if you have.</p>
<h2>Related posts:</h2><ul><li><a href="http://techtracer.com/2008/01/02/wordpress-database-error-table-wp_post2cat-doesnt-exist/" rel="bookmark" title="Permanent Link: Wordpress Database Error &#8211; Table &#8216;wp_post2cat&#8217; doesn&#8217;t exist">Wordpress Database Error &#8211; Table &#8216;wp_post2cat&#8217; doesn&#8217;t exist</a></li><li><a href="http://techtracer.com/2007/07/18/wordpress-error-allowed-memory-size-of-8388608-bytes/" rel="bookmark" title="Permanent Link: Wordpress Error &#8211; &#8220;Allowed memory size of 8388608 bytes&#8221;">Wordpress Error &#8211; &#8220;Allowed memory size of 8388608 bytes&#8221;</a></li><li><a href="http://techtracer.com/2007/08/19/working-with-jax-ws-in-jdk-16/" rel="bookmark" title="Permanent Link: Working With JAX-WS in JDK 1.6">Working With JAX-WS in JDK 1.6</a></li><li><a href="http://techtracer.com/2007/08/01/j2ee-or-jee-java-5-or-java-15-is-sun-crazy/" rel="bookmark" title="Permanent Link: J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?">J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?</a></li><li><a href="http://techtracer.com/2008/06/09/3-easy-steps-to-install-mysql-on-windows-xp/" rel="bookmark" title="Permanent Link: 3 Easy Steps to Install MySQL on Windows XP">3 Easy Steps to Install MySQL on Windows XP</a></li></ul><br /><a href="http://techtracer.com/">Techtracer.com</a> Copyright &copy; 2008<br /> ]]></content:encoded>
			<wfw:commentRss>http://techtracer.com/2007/10/10/resolving-the-unsupported-majorminor-version-490-error/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Understanding XSD Namespaces With Concepts of Java</title>
		<link>http://techtracer.com/2007/09/19/understanding-xsd-namespaces-with-concepts-of-java/</link>
		<comments>http://techtracer.com/2007/09/19/understanding-xsd-namespaces-with-concepts-of-java/#comments</comments>
		<pubDate>Wed, 19 Sep 2007 17:49:17 +0000</pubDate>
		<dc:creator>nitinpai</dc:creator>
				<category><![CDATA[Concepts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://techtracer.com/2007/09/19/understanding-xsd-namespaces-with-concepts-of-java/</guid>
		<description><![CDATA[This article is aimed at developers who are good at programming languages like Java and want a quick jump on the XSD bandwagon by understanding some important concepts like namespaces. Hope that this article helps to make you understand namespaces quite easily.
What is a namespace?
A namespace is an identifier for elements. XML and XSD use [...]]]></description>
			<content:encoded><![CDATA[<p>This article is aimed at developers who are good at programming languages like <strong>Java</strong> and want a quick jump on the <strong>XSD</strong> bandwagon by understanding some important concepts like <strong>namespaces</strong>. Hope that this article helps to make you understand <strong>namespaces</strong> quite easily.</p>
<p><u><strong>What is a namespace?</strong></u></p>
<p>A namespace is an <strong>identifier</strong> for elements. <strong>XML</strong> and <strong>XSD</strong> use the concept of namespaces to define a<strong> relationship</strong> between the elements and their hierarchy. It means that when their are multiple elements to be defined then it might be possible that some of the elements may tend to have the same name. But having the same name often leads to collision just as the in the case of classes with same names although in different packages but referenced by name alone in a Java program. Hence the concept of <strong>namespaces</strong> is used in order to distinguish such elements.</p>
<p><u><strong>Why are namespaces used?</strong></u></p>
<p>XML and XSD are never meant for human reading. You have to remember that even though XML is a <strong>data representation</strong> markup language, it would be rare that you will actually be parsing through an XML file to use the data in your application through your own code.</p>
<p>Such parsing is often supplied through the use of API&#8217;s like <strong>SAX, DOM, JAXB</strong>, etc. You would have till now actually used these libraries while dealing with web services since it is field which uses XML, XSD to the maximum. The concept of namespace has no <strong>relevance</strong> until and unless an XML<strong> instance</strong> is associated with an <strong>XML Schema</strong> (XSD). This is because, XSD defines what do the elements in the XML instance actually mean. A standalone XML just makes the data easier to understand to you but to an XML parser it is nothing but a set of elements in some heirarchy.</p>
<p>For example if suppose you have the following XML format:</p>
<blockquote><p>&lt;employee&gt;<br />
&lt;name&gt;<strong>Alan</strong>&lt;/name&gt;<br />
&lt;/employee&gt;</p></blockquote>
<p>From this an XML parser can list out that the XML instance contains <strong>2 elements</strong> in which <em>employee</em> is the <strong>root </strong>and <em>name</em> is the <strong>child</strong> of employee. It cannot say anything more than that. For giving more meaning to the elements, XML Schema or XSD should be provided. If you want the parser to know what kind of element <em>employee</em> is then you would have to provide a namespace for the element to be in as follows:</p>
<blockquote><p>&lt;<strong>tt</strong>:employee <strong>xmlns:tt</strong>=&#8221;http://techtracer.com/schemas/employee&#8221;&gt;<br />
&lt;name&gt;Alan&lt;/name&gt;<br />
&lt;/<strong>tt</strong>:employee&gt;</p></blockquote>
<p><u><strong>How to interpret namespaces?</strong></u></p>
<p>In an object oriented language like Java, entities of the real world are mapped to <strong>Beans</strong>. Beans have <strong>properties</strong>. And in general every application uses the beans and their properties to provide values which are taken from either a system or an end user. Such values when needed to be transferred in the XML format need some kind of hierarchy to be structured with. For this API&#8217;s come to the rescue.</p>
<p>But before we can supply the API with the elements in any adhoc fashion it becomes necessary that we make the API aware of the structure by providing it with the Schema without which the API won&#8217;t be able to tell you if anything has gone wrong in the hierarchy or not. Such API are said to be <strong>namespace aware</strong> API&#8217;s.</p>
<p>For Example, if I have an bean inside a package which is to be included into any program then you would include it when required as:</p>
<blockquote><p><strong>import</strong> com.techtracer.*;</p>
<p>//and then make the bean as</p>
<p>MyBean bean = new MyBean();</p></blockquote>
<p>Now you can refer to the classes within the package successfully.</p>
<p>Comparing the above to namespaces consider the example.</p>
<blockquote><p>&lt;tt:employee xmlns:tt=&#8221;http://techtracer.com/schemas/employee&#8221;&gt;<br />
&lt;/tt:employee&gt;</p></blockquote>
<p>Here <strong>tt</strong> would be an <em>prefix</em> to the elements which has the value of the entire URL and it would correspond to the namespace just as a package in Java and <em>employee</em> would be an element just as the <em>MyBean</em> in Java. If you would not have provided the packages then the Java program would have given out an error. Similarly in a namespace aware XML parser if you don&#8217;t specify the namespace of the element then it would not run successfully.</p>
<p><u><strong>Default Namespaces</strong></u></p>
<p><strong>java.lang.*</strong> is the default package of core java. Comparing with a XSD the default namespace is:</p>
<blockquote><p>&lt;xsd <strong>xmlns</strong>=&#8221;http://www.w3c.org/2001/01/XMLSchema&#8221;&gt;<br />
&lt;/xsd&gt;</p></blockquote>
<p>Here <strong>xsd</strong> is in the default namespace i.e. the element xsd is an element of the XSD <strong>specification</strong>. So now we can say that as java.lang is the default package of java similarly the above namespace is the default namespace of XSD. And as <strong>Object</strong> is the parent of the Java classes the element xsd is the root of the XSD specification</p>
<p><u><strong>Custom and Target Namespaces</strong></u></p>
<p>There are libraries in Java, which may / may NOT be required. Library means something which you can refer to when required, else not. So including it unnecessarily as a inherent feature may be disturbing since they are present for specific reasons and not to be imposed as an implicit feature of the language. Consider this example:</p>
<blockquote><p>&lt;xsd xmlns=&#8221;http://www.w3c.org/2001/01/XMLSchema&#8221;&gt;<br />
&lt;<strong>employee</strong>&gt;Alan&lt;/<strong>employee</strong>&gt;<br />
&lt;/xsd&gt;</p></blockquote>
<p>Woops, now here is an error. The element <em>employee</em><strong> </strong>is <strong>NOT</strong> an element of the XSD specification. If you have an XSD<strong> validator</strong> it will blurt out with a big error message. This is because the XSD specification does not know a thing about your custom element <em>employee</em>. So what do you do? Somehow you should keep your<strong> </strong><em>employee</em> in a safe and secure place where it can be recognized as yours. So we can keep it under a <strong>custom namespace</strong> like &#8220;http://techtracer.com/schemas/employee&#8221;. So the change required in the above fragment would be as follows:</p>
<blockquote><p>&lt;xsd xmlns=&#8221;http://www.w3c.org/2001/01/XMLSchema&#8221; <strong>targetnamespace</strong>=&#8221;http://techtracer.com/schemas/employee&#8221;&gt;<br />
&lt;employee&gt;Alan&lt;/employee&gt;<br />
&lt;/xsd&gt;</p></blockquote>
<p>So now when the XSD validator comes across the element <strong>employee</strong>, it just checks if you have told it where it should belong (before starting the ritual of swearing at you!). Now it knows that you have mentioned a thing called <strong>targetnamespace.</strong> So it knows that your <em>employee</em> actually belongs to a separate specification which is nothing but your custom XML Schema. The new URL which is mentioned in the targetnamespace is your <strong>custom namespace</strong> itself.</p>
<p><u><strong>Conclusion</strong></u></p>
<p>Hope that this article helps to make you understand <em>namespaces</em> quite easily. Feel free to ask a question if you have any doubt about <em>namespaces</em>. Your query will be solved at the earliest.</p>
<h2>Related posts:</h2><ul><li><a href="http://techtracer.com/2007/04/04/java-5-generics-changing-for-good/" rel="bookmark" title="Permanent Link: Java 5 Generics &#8211; Changing for good">Java 5 Generics &#8211; Changing for good</a></li><li><a href="http://techtracer.com/2007/10/10/resolving-the-unsupported-majorminor-version-490-error/" rel="bookmark" title="Permanent Link: Resolving the &#8220;Unsupported major.minor version 49.0&#8243; Error">Resolving the &#8220;Unsupported major.minor version 49.0&#8243; Error</a></li><li><a href="http://techtracer.com/2007/08/13/java-ee-exhaustive-yet-enchanting/" rel="bookmark" title="Permanent Link: Java EE &#8211; Exhaustive yet Enchanting">Java EE &#8211; Exhaustive yet Enchanting</a></li><li><a href="http://techtracer.com/2007/08/01/j2ee-or-jee-java-5-or-java-15-is-sun-crazy/" rel="bookmark" title="Permanent Link: J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?">J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?</a></li><li><a href="http://techtracer.com/2007/09/03/and-i-thought-i-knew-how-java-worked/" rel="bookmark" title="Permanent Link: And I thought I Knew How Java Worked!">And I thought I Knew How Java Worked!</a></li></ul><br /><a href="http://techtracer.com/">Techtracer.com</a> Copyright &copy; 2008<br /> ]]></content:encoded>
			<wfw:commentRss>http://techtracer.com/2007/09/19/understanding-xsd-namespaces-with-concepts-of-java/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>And I thought I Knew How Java Worked!</title>
		<link>http://techtracer.com/2007/09/03/and-i-thought-i-knew-how-java-worked/</link>
		<comments>http://techtracer.com/2007/09/03/and-i-thought-i-knew-how-java-worked/#comments</comments>
		<pubDate>Mon, 03 Sep 2007 16:00:08 +0000</pubDate>
		<dc:creator>nitinpai</dc:creator>
				<category><![CDATA[Concepts]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://techtracer.com/2007/09/03/and-i-thought-i-knew-how-java-worked/</guid>
		<description><![CDATA[I did till today, and realized that many concepts are still unknown to me as well as some of my friends who have worked on Java EE for a long time now. I have learnt many frameworks along the course from the day I started my professional work in J2EE (now christened JEE).  Java [...]]]></description>
			<content:encoded><![CDATA[<p>I did till today, and realized that many concepts are still unknown to me as well as some of my friends who have worked on <a href="http://techtracer.com/2007/08/13/java-ee-exhaustive-yet-enchanting/">Java EE</a> for a long time now. I have learnt many <strong>frameworks</strong> along the course from the day I started my professional work in J2EE (now <a href="http://techtracer.com/2007/08/01/j2ee-or-jee-java-5-or-java-15-is-sun-crazy/">christened JEE</a>).  Java in its core form never came across me as I was eager to find out the challenges that I faced while working with JEE. It is sometimes <strong>interesting</strong> to see your much loved aspect, turning out on your face and looking like a complete stranger to you.</p>
<p>It was in the morning that my colleague asked me that he had taken an <strong>interview</strong> where he asked the question to the interviewee,</p>
<blockquote><p>What is the difference between JVM and JRE?</p></blockquote>
<p>Of course, it was <strong>Java Virtual Machine</strong> and <strong>Java Run Time Environment</strong>, I said without a second thought. But it doomed to me like never before, what next was their difference? Was it something that <strong>SUN</strong> had planned to demystify my confidence in the world of <strong>Java</strong>. Reluctantly, yet diplomatically I said to him that I knew the difference but I would like his point of opinion. He said something which I half heartedly believed, putting on a note in my mind the quest for the day to find the interesting answer to this seemingly easy but notorious question. The answer he relented was,</p>
<blockquote><p>JVM is a <strong>specification</strong> and JRE is an<strong> implementation</strong>. JVM is just like the Servlet or the JSP specification provided by SUN and JRE is the implementation that is OS specific. JVM is <strong>not</strong> installed, but JRE is.</p></blockquote>
<p>The last line left me gasping for breath. Until now I had been installing JVM and also had seen the <strong>JRE</strong> folder in <strong>C:/Java</strong>, but today I failed to recollect what the heck was JRE and how in the world I could install a <strong>JVM</strong>, as supposedly said by my colleague.</p>
<p>I went to the office to quench the thirst of my curiosity. I was ablaze with <strong>surprise </strong>when I searched and found the exact same words that my colleague had said to me. This came from the <a href="http://forum.java.sun.com/thread.jspa?threadID=593100&amp;messageID=3107121" target="_blank">Java Forum</a>. I thought it was maybe the right one. Enlightened with this, I called upon another colleague of mine and asked him as to put him to the judgment grill. The question remained the same but the answer did not. It was another baffling one,</p>
<blockquote><p>Do you know you can download JVM too. Now don&#8217;t try to act smart. Just Google it and you will come to know.</p></blockquote>
<p>And Google did answer it by religiously giving me the set of links to <a href="http://www.google.co.in/search?q=download+JVM&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a" target="_blank">download JVM</a>. But none of them were from the creator itself i.e SUN. It was turning out a little <strong>fishy</strong> in this. Now came the moment to test <a href="http://www.google.co.in/search?q=download+JRE&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a" target="_blank">download JRE</a> and to my relief, the first link was of SUN. Thankfully the query had been <strong>resolved</strong> to a partial satisfaction that maybe the first answer had really been true.</p>
<p>Little was my confidence boosted when my colleague intriguingly asked me another,</p>
<blockquote><p>Did you know that your JRE is capable of running your class files without the need of a JDK. I have done that and they ran like any normal program would.</p></blockquote>
<p>I gawked at him in <strong>amazement</strong>. It was like getting <strong>bedazzled </strong>with someone pounding you like a heavy weight boxer. But having no mercy he went on saying,</p>
<blockquote><p>I have been able to run Eclipse and write Java programs without having JDK in my system. I have seen there is no <strong>javac</strong> in JRE folder but still I was able to create a Java Project and run it in Eclipse.</p></blockquote>
<p>Bewilderment was taking a toll on my brain and I was at the brink of saying that I love <strong>Microsoft</strong> and <strong>.NET</strong>, when he said that he was also amazed at this but did not know the right answer. Phew! something to cheer about, at last. It is good to know that somebody else in your surrounding is feeling the same pain in the butt and telling you about it.</p>
<p>One thing was clear, my knowledge lacked the essence of<strong> core Java</strong> as some of them who searched the forums to find a relieving answer. And as lucky as I could get I got one such nicely explained statement from which, I hopefully came to know what was the difference between JVM and JRE, in <a href="http://www.jguru.com/faq/view.jsp?EID=46212" target="_blank">JGuru forum</a>,</p>
<blockquote><p>JVM forms part of large system JRE. It comprises a set of base class which are implementation of base java API, as well as JVM.  JVM is used for running the java software with the availablity of JRE which is different in a given environment. JVM&#8217;s main job is interpreting java bytecode and translating this into actions or OS calls.</p></blockquote>
<p>This I found was the correct definition and further to clear my doubts I found one more resource which gave out a<strong> formal explanation</strong> with a set of diagrams to clarify the point being made. The point of concern are as follows:</p>
<ul>
<li>The Java Virtual Machine is responsible for <strong>interpreting</strong>         Java<strong> bytecode</strong>, and translating this into actions or operating system         call.</li>
<li>The Java Virtual Machine forms <strong>part</strong> of a large system,         the Java Runtime Environment (JRE). Each operating system and CPU         architecture requires a <strong>different JRE</strong>.</li>
<li>The JRE comprises a set of <strong>base         classes</strong>, which are an implementation of the base Java API, as well as a         <strong>JVM</strong></li>
<li>Without an available JRE for a given         environment, it is <strong>impossible</strong> to run Java software.</li>
</ul>
<p>In short and sweet words, I can now say that JRE is like the <strong>motherboard</strong> and JVM is like your <strong>processor</strong> which powers your <strong>computer</strong>. You can never have a processor and motherboard catering to different architecture. And although each entity is a different <strong>component</strong> of your computer, both have to run in <strong>tandem</strong> to bring the power of computing to reality.</p>
<p>Now I somewhat know how <strong>Java</strong> works!</p>
<h2>Related posts:</h2><ul><li><a href="http://techtracer.com/2007/10/31/mozilla-prism-a-revolution-in-web-apps/" rel="bookmark" title="Permanent Link: Mozilla Prism &#8211; A Revolution in Web Apps">Mozilla Prism &#8211; A Revolution in Web Apps</a></li><li><a href="http://techtracer.com/2007/07/09/scwcd-mission-accomplished-2/" rel="bookmark" title="Permanent Link: SCWCD &#8211; Mission Accomplished 2">SCWCD &#8211; Mission Accomplished 2</a></li><li><a href="http://techtracer.com/2008/01/23/effects-of-oracle-bea-acquisition/" rel="bookmark" title="Permanent Link: Effects of Oracle BEA Acquisition">Effects of Oracle BEA Acquisition</a></li><li><a href="http://techtracer.com/2007/08/26/eclipse-europa-has-ruined-my-day/" rel="bookmark" title="Permanent Link: Eclipse Europa Has Ruined My Day">Eclipse Europa Has Ruined My Day</a></li><li><a href="http://techtracer.com/author/" rel="bookmark" title="Permanent Link: Author">Author</a></li></ul><br /><a href="http://techtracer.com/">Techtracer.com</a> Copyright &copy; 2008<br /> ]]></content:encoded>
			<wfw:commentRss>http://techtracer.com/2007/09/03/and-i-thought-i-knew-how-java-worked/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?</title>
		<link>http://techtracer.com/2007/08/01/j2ee-or-jee-java-5-or-java-15-is-sun-crazy/</link>
		<comments>http://techtracer.com/2007/08/01/j2ee-or-jee-java-5-or-java-15-is-sun-crazy/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 17:12:53 +0000</pubDate>
		<dc:creator>nitinpai</dc:creator>
				<category><![CDATA[Concepts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java EE]]></category>

		<guid isPermaLink="false">http://techtracer.com/2007/08/01/j2ee-or-jee-java-5-or-java-15-is-sun-crazy/</guid>
		<description><![CDATA[Will they take a J2EE professional or a JEE professional now is the thing I am worrying about. It is obvious for a technical person to know that J2EE and JEE are one and the same except for the HR persons! How do we go about convincing them? Many people including me, still are confused [...]]]></description>
			<content:encoded><![CDATA[<p>Will they take a <strong>J2EE</strong> professional or a <strong>JEE</strong> professional now is the thing I am worrying about. It is obvious for a technical person to know that J2EE and JEE are one and the same except for the HR persons! How do we go about convincing them? Many people including me, still are confused about the version and the mysterious number attached to the &#8220;<strong>Java</strong>&#8221; word. Let&#8217;s try to explore what actually goes on behind these mysteries. Check this out!</p>
<p><u><strong>The product confusion</strong></u></p>
<p>If you are still thinking that <strong>Java 2</strong> meant version <strong>2</strong> of Java then you are being duped. Java 2 means version <strong>1.4 </strong>of Java. I am still dumbfounded as to where <strong>Java 3</strong> and <strong>Java 4</strong> disappeared but I guess with the success of <strong>Java 5</strong> they were buried in debris even before they born!</p>
<p>I was still in bewilderment until the web started to roar about <strong>Java 6</strong>. It took me a lot of time to actually sink in the fact whether Java 5 referred to version 1.5 of Java and Java 6 referred to version 1.6 of Java. This confusion was due to the use of Java 5.0 in some places. Are Java 5.0 and version 1.5 of Java one and the same?</p>
<p>The fact is that <a href="http://java.sun.com/j2se/1.5.0/docs/relnotes/version-5.0.html" target="_blank">Sun</a> makes use of dual naming convention for the same thing. One naming convention if for the product and one is for the developer. The official documents mentions,</p>
<blockquote><p>Both version numbers &#8220;1.5.0&#8243; and &#8220;5.0&#8243; are used to identify this release of the Java 2 Platform Standard Edition. Version &#8220;5.0&#8243; is the product version, while &#8220;1.5.0&#8243; is the developer version. The number &#8220;5.0&#8243; is used to better reflect the level of maturity, stability, scalability and security of the J2SE.</p></blockquote>
<p>Really, a lot of maturity I say!</p>
<p><u><strong>Developmental and Mental Destruction</strong></u></p>
<p>SUN has not left people in peril by their naming conventions swapping not only in the case of Java releases but also in the names of the <strong>development kits</strong>. Earlier for programming in Java I used to download <strong>JDK</strong> which meant <em>Java Development Kit</em>. I don&#8217;t remember which version I was working upon. Maybe it was 1.1.</p>
<p>But when the Java 2 reign started I was asked to download <strong>J2SDK</strong> which means <strong>Java 2 Software Development Kit</strong>. SUN had finally realized that people were actually making softwares on Java!!</p>
<p>Java 2 was there to stay for a long time until the hue and uproar for Java 5 came in the picture. But wait, SUN was ready for another swap of naming to gain momentum with the success Java 5, which was reverting back to the name of JDK.<br />
The official document mentions,</p>
<blockquote><p>Due to significant popularity within the Java developer community, the development kit has reverted back to the name &#8220;JDK&#8221; from &#8220;Java 2 SDK&#8221;</p></blockquote>
<p>Am I supposed to NOW assume if JDK still stands for the same meaning i.e &#8220;<em>Java Development Kit</em>&#8220;. No! Sun has more surprises for me. NOW, JDK actually means &#8220;<strong>J2SE Development Kit</strong>&#8220;. I still fear when this meaning will come crashing by! Did I hear some one shouting &#8220;<em>Jean Claude Van Damn Karate Club</em>&#8221; for JDK!!</p>
<p><u><strong>Certifications Confusion</strong></u></p>
<p>The certifications were also not spared. If you happen to prepare yourself for the <strong>SCBCD</strong> (Sun Certified Business Component Developer) exam, then you are in for trouble with making sure which exam are you heading for. I bet you would definitely get fumbled in this-</p>
<ul>
<li> SCBCD <strong>1.3</strong> refers to you working with <strong>EJB 2.0</strong></li>
<li>SCBCD <strong>5.0</strong> refers to you working with <strong>EJB 3.0</strong></li>
</ul>
<p>Didnt it all appear to SUN to keep the version of SCBCD same as the EJB we are working on?</p>
<p>The reality is that EJB <strong>2.x</strong> was meant to work with <strong>J2EE 1.3</strong> and since now after the up gradation of EJB  to EJB <strong>3.0</strong> even the Java EE specifications have been upgraded to <strong>5.0</strong>. Hence the certification number is the number of the Java EE specification and not of the version of EJB it is based on. Similarly it is SCJP <strong>1.4</strong>  and SCWCD <strong>1.4</strong> which are for J2EE.. err.. I mean J2EE 1.4 or is it so. Jesus, pardon me if I have done anything wrong!</p>
<p>Now you must have been enlightened about which version you are associated with. I will take some time to digest this completely. My head is still reeling with these numerological mumbo jumbo. Maybe there is something more in store for us. Before that hey, which version are you currently working on?</p>
<h2>Related posts:</h2><ul><li><a href="http://techtracer.com/2007/09/03/and-i-thought-i-knew-how-java-worked/" rel="bookmark" title="Permanent Link: And I thought I Knew How Java Worked!">And I thought I Knew How Java Worked!</a></li><li><a href="http://techtracer.com/2007/04/04/java-5-generics-changing-for-good/" rel="bookmark" title="Permanent Link: Java 5 Generics &#8211; Changing for good">Java 5 Generics &#8211; Changing for good</a></li><li><a href="http://techtracer.com/2007/03/15/scdjws-preparation-a-kick-start-guide/" rel="bookmark" title="Permanent Link: SCDJWS preparation &#8211; a kick start guide">SCDJWS preparation &#8211; a kick start guide</a></li><li><a href="http://techtracer.com/2007/03/17/scdjws-study-material-and-resources-the-most-searched-thing/" rel="bookmark" title="Permanent Link: SCDJWS study material and resources &#8211; The most searched thing">SCDJWS study material and resources &#8211; The most searched thing</a></li><li><a href="http://techtracer.com/2007/01/29/scdjws-mission-accomplished/" rel="bookmark" title="Permanent Link: SCDJWS &#8211; mission accomplished">SCDJWS &#8211; mission accomplished</a></li></ul><br /><a href="http://techtracer.com/">Techtracer.com</a> Copyright &copy; 2008<br /> ]]></content:encoded>
			<wfw:commentRss>http://techtracer.com/2007/08/01/j2ee-or-jee-java-5-or-java-15-is-sun-crazy/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>SCWCD &#8211; The 3 Steps to Success!</title>
		<link>http://techtracer.com/2007/07/10/scwcd-the-3-steps-to-success/</link>
		<comments>http://techtracer.com/2007/07/10/scwcd-the-3-steps-to-success/#comments</comments>
		<pubDate>Tue, 10 Jul 2007 16:53:40 +0000</pubDate>
		<dc:creator>nitinpai</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[SCWCD]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://techtracer.com/2007/07/10/scwcd-the-3-steps-to-success/</guid>
		<description><![CDATA[After my completion of SCDJWS I had written about the 3 steps to success in SCDJWS which has helped many who were new in the web services domain to help them get prepared and confident about preparing for the big exam! Following the same foot steps I am now presenting in front of you 3 [...]]]></description>
			<content:encoded><![CDATA[<p>After my completion of <strong>SCDJWS</strong> I had written about the <a href="/category/scdjws/">3 steps to success</a> in <strong>SCDJWS</strong> which has helped many who were new in the web services domain to help them get prepared and confident about preparing for the big exam! Following the same foot steps I am now presenting in front of you <strong>3 steps</strong> to success in <strong>SCWCD</strong>. I scored a <strong>90+</strong> <a href="/2007/07/09/scwcd-mission-accomplished-2/">in the exam</a> and so I take a a pride to provide you the guidance of how to get a <strong>90+ </strong></p>
<p><strong>Why are there always 3 Steps to success?</strong></p>
<p>This probably would be the same doubt on the lines of why do we say &#8220;<em>Ready&#8230;Steady&#8230;Go</em>&#8221; and also the countdown of a start of a timer in &#8220;<em>3..2..1</em>&#8220;. Well, the 3 steps are same everywhere, only their definition changes. Similarly I always make it a point to divide my preparation routine into particularly 3 steps to make it sound quicker and much systematic. Finally, its the enthusiasm that counts that you put in each of the steps.</p>
<p><strong>Stop all that blabber&#8230; How do I go about preparing for SCWCD?</strong></p>
<p>Okay..okay. <a href="http://www.sun.com/training/certification/java/scwcd.xml">SCWCD</a> (exam code &#8211; <strong>CX 310 081</strong>) is aimed particularly for the web developers and it takes into consideration that you must have developed web applications in the past and now want to try out with <strong>JSP/ Servlets</strong>. So there wont be any questions about how and what are the intricacies dealing with what a web application is, but concentrates on how would you build web applications using the web components that <strong>SUN</strong> provides.</p>
<p><u><strong>SCWCD &#8211; The preparation</strong></u></p>
<p>The 3 steps for success in <strong>SCWCD</strong> are: </p>
<p><u><strong>Step 1 </strong><em><strong>- </strong></em><strong>Head First Servlets &amp; JSP<br />
</strong></u></p>
<p style="text-align: center"><img src="http://techtracer.com/wp-content/uploads/2007/07/hfsj.gif" alt="hfsj.gif" /></p>
<p>The <strong>Head First</strong> Series have become a kind of bible series in whatever they have released their books. In case of JSP / Servlets , its remarkable to see how easily the concepts can be grasped from their way of story telling. Some people would find their books very nonsensical with all those cluttered animations and text written like a scratch pad. Even I had felt the same before reading their first chapter. I did not even come to know that I had finished a total of 3 chapters even though I had the intention of reading just one.</p>
<p>You should mark my words &#8220;<em>Every beginner must use the Head First Series to grasp the concepts quickly</em>&#8220;. Ever since I have read this book I have become a fan of the authors! This book would even be handy to pass out of the exam if you were actually acquainted with working of JSP / Servlets. Solve all the questions at the end of each chapter.</p>
<p><em><strong>OR</strong></em></p>
<p><u><strong>Step 1 </strong><em><strong>-</strong></em><strong> SCWCD Exam Study Kit &#8211; Hanumant Deshmukh</strong></u></p>
<p style="text-align: center"><img src="http://techtracer.com/wp-content/uploads/2007/07/scwcd_exam_study_kit.gif" alt="scwcd_exam_study_kit.gif" /></p>
<p>For a beginner in the world of JSP/ Servlets the Head First Series is enough. But for people who have worked for some time in this technology would find the Head First Series a no brainer. Instead I would suggest such people to get a copy of this book. First of all, its a no nonsense book. It goes strictly with the exam topics and covers almost everything that is required to pass out of the exam.</p>
<p>The language is very lucid and is devoid of any jargons. The quizzes at the end of each chapter should be solved only after you have read the entire book since it would be like a revision of all the concepts. I especially liked the<strong> quizlets</strong> in the midst of the chapters which act as brain teasers. It even gives you a quick revision section where in you can just flip through the pages and have a quick briefing of all the important aspects for the exam. It does not have a mock test as HFSJ does at the end of all chapters.</p>
<p><u><strong>Step 2 &#8211; SCWCD Study Companion &#8211; Charles Lyons</strong></u></p>
<p align="center"><img src="http://techtracer.com/wp-content/uploads/2007/07/scwcd_study_companion.gif" alt="scwcd_study_companion.gif" /></p>
<p>SCWCD <strong>cannot</strong> be aimed for a <strong>100%</strong> if you have not referred to the <strong>API documentation</strong> or the <strong>JSP and Servlet Specifications.</strong> But would you really like to go through such humongous documentation within a short span of time? I hear many no&#8217;s cried out loud. But what if I say you would get a compact series of chapters and exam wise listed topics with the API information just perfect for the exam and Specification filtered out with only the exam relevant aspects then this the book to go for. I woudln&#8217;t have been able to score a <strong>90+</strong> score seriously speaking without this book.</p>
<p><a href="http://www.garnerpress.com/catalogue/BK0316"><strong>Charles Lyons</strong></a> has prepared the book with a lot of efforts of perfectly scanning upto the <strong>API level details</strong> and also packaging it with the <strong>specification information</strong> so compactly that you don&#8217;t get bored reading it. He has maintained the flow so accurately that your questions will immediately be answered just as they get raised in your mind. <strong>Don&#8217;t</strong> try to read through the chapters in between because the flow of the chapters will be clear only when you start it from the beginning. I <strong>assure</strong> that reading this book wont make you touch the specifications nor the API documentations at all. Even I didn&#8217;t!</p>
<p>The best part of the book is it acts not only as a SCWCD study companion but also as a <strong>Reference book</strong> for future use since it has additional chapters for the <strong>updates </strong>that have happened in the recent past in JSP and Servlets.</p>
<p>The one thing I would reprimand in using this book is<strong> don&#8217;t</strong> try to solve tests provided at the end of each chapter since they are literally tough! You need to have your brain super charged to solve the tests since they cover an amazing depth of knowledge. But on the lines of SCWCD you <strong>won&#8217;t </strong>get such difficult questions so it is better if you stick to the tests provided in the earlier 2 books.</p>
<p><u><strong>Step 3 &#8211; Test yourself with the JWebPlus Simulator and the HFSJ mock test</strong></u></p>
<p style="text-align: center"><img src="http://techtracer.com/wp-content/uploads/2007/07/jwebplus.gif" alt="jwebplus.gif" /></p>
<p>Finally when you are exhausted with all the reading you would need some time to practice. Till now I have not said about practicing! This is because I believe that it is better if you first understand all the concepts clearly before practicing. So after all your theoretical concepts have been cleared try to practice <strong>on the basis of mock tests</strong>! This is a unique way of clearing your doubts.</p>
<p>The <strong>JWebPlus Simulator</strong> is the <strong>best</strong> simulator, provided by <a href="http://www.enthuware.com/jwebplus/">Enthuware</a>, which perfectly mimics the original exam environment. I haven&#8217;t tried the <strong>Whizlabs</strong> since they don&#8217;t replicate the same sense of depth as the exam provides. They are more tough than the actual exam and I don&#8217;t want anyone to feel disheartened when they get a low WhizLabs score.</p>
<p>After your reading is over try to solve 1 or 2 tests. In each test <strong>note down</strong> which are your strong and weak points. The exam results provide scores topic wise. The strong points dont need to be tried out since you obviously have the knowledge of them. But you definitely need to try out the weak points.</p>
<p>If you ask me I had failed in the first 2 tests and my weak points were mainly tag libraries and EL(Expression Language). But when I realized that I had the theoretical knowledge but did not know about their practical aspects then I tried it out myself and each of the trials clarified my doubts and I felt more confident. I sincerely feel this is a good approach rather than practicing out each and every thing. Some things can be just learnt from the books.</p>
<p>The JWebPlus Simulator has a total of <strong>7 mock tests</strong> and <strong>3 categorical tests</strong> namely &#8211; easy, tough and very tough. By carrying out the step in the fashion as I mentioned I started getting 84% in the later tests and my confidence was sky high.</p>
<p>The ultimate test I would advice you to give is the <strong>HFSJ mock test</strong>. Altough after solving so many tests you don&#8217;t actually have to solve this one, but as the saying goes &#8220;<em>Nothing is impossible</em>&#8221; you should try to solve it out too. The only challenge you would face here is that the multiple choice questions haven&#8217;t been stated as to how many you have to choose. Rather it is mentioned &#8220;<em>Choose all that apply</em>&#8221; which is somewhat irritating. But you should accept it as a challenge and prove yourself that you are an <strong>expert</strong> now! I took the challenge and scored an 81% in the mock test.</p>
<p><u><strong>The battle begins!</strong></u></p>
<p style="text-align: center"><img src="http://techtracer.com/wp-content/uploads/2007/07/scwcd.gif" alt="scwcd.gif" /></p>
<p>So now that your 3 steps have completed, you are armed and ready to conquer the test of the time. The final <strong>SCWCD</strong> (<strong>CX</strong> <strong>310 081</strong> &#8211; remember this!) and the moment of celebration will be just 2 and a half hours far from you! So get armed with these 3 steps in mind and jump the bandwagon of conquerors as I shout out loud &#8220;<em>Ready&#8230;Steady&#8230;Go!</em>&#8220;</p>
<h2>Related posts:</h2><ul><li><a href="http://techtracer.com/2008/01/16/ucertify-scwcd-prepkit-detailed-review/" rel="bookmark" title="Permanent Link: uCertify SCWCD PrepKit &#8211; Detailed Review">uCertify SCWCD PrepKit &#8211; Detailed Review</a></li><li><a href="http://techtracer.com/2007/08/01/j2ee-or-jee-java-5-or-java-15-is-sun-crazy/" rel="bookmark" title="Permanent Link: J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?">J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?</a></li><li><a href="http://techtracer.com/2007/07/09/scwcd-mission-accomplished-2/" rel="bookmark" title="Permanent Link: SCWCD &#8211; Mission Accomplished 2">SCWCD &#8211; Mission Accomplished 2</a></li><li><a href="http://techtracer.com/2008/10/06/google-chrome-whats-in-it-for-ya/" rel="bookmark" title="Permanent Link: Google Chrome &#8211; What&#8217;s in it for ya?">Google Chrome &#8211; What&#8217;s in it for ya?</a></li><li><a href="http://techtracer.com/2007/04/09/web-and-desktop-apps-the-3rd-world-war/" rel="bookmark" title="Permanent Link: Web and Desktop Apps &#8211; the 3rd World War!">Web and Desktop Apps &#8211; the 3rd World War!</a></li></ul><br /><a href="http://techtracer.com/">Techtracer.com</a> Copyright &copy; 2008<br /> ]]></content:encoded>
			<wfw:commentRss>http://techtracer.com/2007/07/10/scwcd-the-3-steps-to-success/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>The Struts Framework &#8211; Why choose it?</title>
		<link>http://techtracer.com/2007/06/03/the-struts-framework-why-choose-it/</link>
		<comments>http://techtracer.com/2007/06/03/the-struts-framework-why-choose-it/#comments</comments>
		<pubDate>Sun, 03 Jun 2007 16:49:44 +0000</pubDate>
		<dc:creator>nitinpai</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java EE]]></category>

		<guid isPermaLink="false">http://techtracer.com/2007/06/03/the-struts-framework-why-choose-it/</guid>
		<description><![CDATA[Struts is a J2EE web application framework created and maintained by Apache Software Foundation Group (ASF). It is a controller framework based on the Front Controller pattern and used to create an MVC (Model View Controller) architecture.
 What is a Front Controller Pattern?
A Front controller pattern follows the rule of having a single entity controlling [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://struts.apache.org">Struts</a> is a J2EE web application framework created and maintained by Apache Software Foundation Group (<strong>ASF</strong>). It is a controller framework based on the <strong>Front Controller</strong> pattern and used to create an <strong>MVC</strong> (Model View Controller) architecture.</p>
<p><strong> What is a Front Controller Pattern?</strong></p>
<p>A Front controller pattern follows the rule of having a single entity controlling the entire application. The single entity acts as a barrier between the client and the remote application. All requests targeted towards the application and first received by the single entity and then based on the controlling flow of the application, the requests are forwarded to the intended recipient module of the application.</p>
<p>The advantage of the Front Controller Pattern is in the simplicity of maintaining a single entity instead of multiple flows controlled by multiple entities. This results in streamlining the flow and proper filtering of the requests made to the application.</p>
<p><strong>Why is Struts Framework essential in an MVC architecture?</strong></p>
<p>In a J2EE web application, when creating an MVC architecture, usually the JSP is taken for a view while the Servlets do the job of controlling the flow from the view to the underlying model, which may consist of <strong>POJO</strong>&#8217;s or <strong>EJB</strong>&#8217;s.</p>
<p>When it comes to designing the controlling flow for the application, developers are left in a scurry to decide how many Servlets should they finalize for the application.This decision is usually based on the number of forms which have to be created for the application to collect the data.</p>
<p>This is because each form when submitted via a JSP with a POST method has to be handled in a Servlet (This can be done in a JSP also, but it is not a good practice since scripting inside a JSP is a criminal offence!) . Now if the application consists of about 10 forms, handling each form in a separate Servlet seems to be a good choice, but that means creating 10 Servlets!.</p>
<p><strong>Any alternatives? </strong></p>
<p>The way to avoid through the above situation is have less Servlets and use the <em>switch </em>or the <em>if..else</em>  structure to find which request are you serving  for. But over a period of time this nature of creating Servlets can easily lead to creating a mess of conditional statements scattered over the application leading to costly maintainance.</p>
<p>Besides this, the major downside is that as the number of Servlets in your application grows the number of instances to be kept running in the memory also increases. One Servlet spawning many threads is always less heavier than many Servlets having multiple threads.</p>
<p><strong>Why choose the Struts Framework?</strong></p>
<p>Due to the above analogy it might be worth the benefit if having only a Single Controller Servlet which accepts all the requests and then makes the decision of where to forward the request. But, making a manual Front Controller Design may be more time consuming than thought as compared to making multiple servlets.</p>
<p>Hence to avoid all these difficulties, the Struts framework is used to implement the Controller entity in your MVC architecture since it follows the Front Controller pattern. It has been very well establised in the web applications domain for a long time.</p>
<p><strong>What are the advantages of choosing Struts?</strong></p>
<p>The inherent advantage of choosing the Struts Framework is that you get a ready made implementation of the Front Controller pattern. Besides that Struts is a robust and an entirely flexible framework to work with.</p>
<p>Struts gives you the features of</p>
<ul>
<li>Custom class based controlling components</li>
<li>Population of Java Beans through HTML form elements and their validation</li>
<li>XML based configuration for all underlying components</li>
<li>Full support for internationalization through provision of resource bundles for all the HTML elements</li>
<li>Tag libraries for easy integration with your existing JSP&#8217;s and</li>
<li>An elegant way of handling form related errors.</li>
</ul>
<p>Installing the Struts framework just involves including the required <strong>Struts JAR</strong> files in your <em>/WEB-INF/lib</em> and <em>taglibs</em> in your <em>WEB-INF</em> directory. Struts is a very robust and lightweight framework which can run even on a <strong>Tomcat </strong>Web Container.  Struts can come handy when you want to achieve the task of making a web application which not only is easier to maintain but also good in its overall performance.</p>
<h2>Related posts:</h2><ul><li><a href="http://techtracer.com/2008/03/17/key-points-for-choosing-the-best-framework/" rel="bookmark" title="Permanent Link: Key Points for Choosing the Best framework">Key Points for Choosing the Best framework</a></li><li><a href="http://techtracer.com/2007/08/13/java-ee-exhaustive-yet-enchanting/" rel="bookmark" title="Permanent Link: Java EE &#8211; Exhaustive yet Enchanting">Java EE &#8211; Exhaustive yet Enchanting</a></li><li><a href="http://techtracer.com/2007/03/26/first-step-in-ajax-choosing-the-right-framework/" rel="bookmark" title="Permanent Link: First step in Ajax &#8211; Choosing the right framework">First step in Ajax &#8211; Choosing the right framework</a></li><li><a href="http://techtracer.com/2007/04/23/inversion-of-control-for-easy-integration/" rel="bookmark" title="Permanent Link: Inversion of Control &#8211; for easy integration">Inversion of Control &#8211; for easy integration</a></li><li><a href="http://techtracer.com/2007/03/21/5-tips-to-use-ajax-in-the-proper-way/" rel="bookmark" title="Permanent Link: 5 tips to use Ajax in the proper way">5 tips to use Ajax in the proper way</a></li></ul><br /><a href="http://techtracer.com/">Techtracer.com</a> Copyright &copy; 2008<br /> ]]></content:encoded>
			<wfw:commentRss>http://techtracer.com/2007/06/03/the-struts-framework-why-choose-it/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Looping through Collections is fun in Java 5</title>
		<link>http://techtracer.com/2007/05/16/looping-through-collections-is-fun-in-java-6/</link>
		<comments>http://techtracer.com/2007/05/16/looping-through-collections-is-fun-in-java-6/#comments</comments>
		<pubDate>Wed, 16 May 2007 16:46:04 +0000</pubDate>
		<dc:creator>nitinpai</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://techtracer.com/2007/05/16/looping-through-collections-is-fun-in-java-6/</guid>
		<description><![CDATA[Edit: This post was earlier titled &#8220;Looping through Collections is fun in Java 6&#8221; which has now been renamed to &#8220;Looping through Collections is fun in Java 5&#8221; due to the accidental discrepancy of mentioning the feature belonging to Java 6. The mistake is rectified. Thanks for the feedback to all.
The traditional looping for construct [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Edit:</strong> This post was earlier titled &#8220;<em>Looping through Collections is fun in Java 6</em>&#8221; which has now been renamed to &#8220;<em>Looping through Collections is fun in Java 5</em>&#8221; due to the accidental discrepancy of mentioning the feature belonging to Java 6. The mistake is rectified. Thanks for the feedback to all.</p>
<p>The traditional looping <em><strong>for</strong></em> construct has served well for many years in Java. Even today, for collections people still use the traditional approach for looping through them. The simple reason being familiarity. Its evident that the powers provided by the revolutionary addition of features in Java upgrades have yet to see their complete establishment.</p>
<p>Coming to the collections frameworks the other leagues of languages like <strong>PHP</strong>, <strong>JavaScript</strong>, <strong>VB</strong> have provided convenient looping constructs for the <em>for</em> syntax namely the <em>for each block</em>. Java programmers have craved long for the same and its time that it was implemented in the favorite language of millions worldwide. </p>
<p>The collections framework comes with a new <em><strong>for</strong></em> construct or the <strong>for-each block</strong> although the <em>each</em> term is missing from the syntax. Now you don&#8217;t need the much abused <strong><em>Iterator</em></strong> for looping and fretting over the same routines to be written when a collection is received. The for-each block makes it more cooler to write the same construct without the need to obtain an Iterator and with the added feature of <strong>generics</strong>, you don&#8217;t have to <em>typecast</em> the object received from the collection.</p>
<p>The earlier way in which the code for looping through a collection would have been written as,</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">	<span style="color: #003399; font-weight: bold;">List</span> list = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399; font-weight: bold;">String</span> a = <span style="color: #0000ff;">&quot;Hello&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003399; font-weight: bold;">String</span> b = <span style="color: #0000ff;">&quot;World&quot;</span><span style="color: #339933;">;</span>
	list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003399; font-weight: bold;">String</span> s<span style="color: #339933;">;</span>
	<span style="color: #000000;  font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Iterator</span> i = list.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		s = <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span><span style="color: #009900;">&#41;</span> i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>s + <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>OR</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">	<span style="color: #003399; font-weight: bold;">Iterator</span> i = list.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399; font-weight: bold;">String</span> s<span style="color: #339933;">;</span>
	<span style="color: #000000;  font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		s = <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span><span style="color: #009900;">&#41;</span> i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>s + <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The result would print &#8220;Hello World&#8221;.</p>
<p>The <em>Iterator</em> becomes simply unnecessary since it is obvious that a collections require it to get looped. But in the traditional approach the programmer has to make the trouble of introducing it every time he sees a collection. In <strong>Java 5</strong> you would be writing the same block as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">	List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> list = <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399; font-weight: bold;">String</span> a = <span style="color: #0000ff;">&quot;Hello&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003399; font-weight: bold;">String</span> b = <span style="color: #0000ff;">&quot;World&quot;</span><span style="color: #339933;">;</span>
	list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000;  font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> s : list<span style="color: #009900;">&#41;</span>
		<span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>s + <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here you see that looping through the for-each block is so simple that it reduces all the need to construct an <em>Iterator</em>. The <em>generics</em> features make the collections specific to one type of object i.e when you define <strong><em>List&lt;String&gt;</em></strong> you make it explicit that the <em><strong>List</strong></em> would only contain <em><strong>String</strong></em> objects. Of course you would question about losing flexibility by doing this. But <a href="/2007/04/04/java-5-generics-changing-for-good/">generics</a> is a very vast topic to be dealt with and it includes all the features for introducing objects of inheritance in a collection.</p>
<p>Coming down to the main topic i.e the <strong>for-each</strong> construct, here you only have to define the type of object which you will be requiring in your loop. In the example I had made a <em>String</em> collection. So the object I would require to store the intermediate values would be a String object. The object &#8220;<em>s</em>&#8221; of <em>String</em> type does this work of storing the intermediate values.</p>
<p>The syntax &#8220;<em><strong>String s : list</strong></em>&#8221; means that &#8220;<em>for each object of type <strong>String</strong> in the collection <strong>list</strong></em><em>, do loop and store the obtained value in </em><strong>s</strong>&#8220;. Once inside the loop you get the intermediate value in <em><strong>s</strong></em> to be printed. Hence avoiding the need of an <em>Iterator</em> and a type cast for looping inside the new collections framework. Using this feature the code definitely becomes more easier to be written, debug, test and mainly readable.</p>
<h2>Related posts:</h2><ul><li><a href="http://techtracer.com/2007/04/04/java-5-generics-changing-for-good/" rel="bookmark" title="Permanent Link: Java 5 Generics &#8211; Changing for good">Java 5 Generics &#8211; Changing for good</a></li><li><a href="http://techtracer.com/2007/08/15/jax-ws-jaxp-tutorial-building-a-stockquote-web-service-client/" rel="bookmark" title="Permanent Link: JAX-WS + JAXP Tutorial &#8211; Building A StockQuote Web Service Client">JAX-WS + JAXP Tutorial &#8211; Building A StockQuote Web Service Client</a></li><li><a href="http://techtracer.com/2008/11/24/reflection-in-java-simplified/" rel="bookmark" title="Permanent Link: Reflection in Java &#8211; Simplified">Reflection in Java &#8211; Simplified</a></li><li><a href="http://techtracer.com/2007/08/01/j2ee-or-jee-java-5-or-java-15-is-sun-crazy/" rel="bookmark" title="Permanent Link: J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?">J2EE or JEE, Java 5 or Java 1.5 &#8211; Is SUN Crazy?</a></li><li><a href="http://techtracer.com/2007/10/10/resolving-the-unsupported-majorminor-version-490-error/" rel="bookmark" title="Permanent Link: Resolving the &#8220;Unsupported major.minor version 49.0&#8243; Error">Resolving the &#8220;Unsupported major.minor version 49.0&#8243; Error</a></li></ul><br /><a href="http://techtracer.com/">Techtracer.com</a> Copyright &copy; 2008<br /> ]]></content:encoded>
			<wfw:commentRss>http://techtracer.com/2007/05/16/looping-through-collections-is-fun-in-java-6/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Getting started with IoC &#8211; A simplified tutorial</title>
		<link>http://techtracer.com/2007/04/26/getting-started-with-ioc-a-simplified-tutorial/</link>
		<comments>http://techtracer.com/2007/04/26/getting-started-with-ioc-a-simplified-tutorial/#comments</comments>
		<pubDate>Thu, 26 Apr 2007 18:08:22 +0000</pubDate>
		<dc:creator>nitinpai</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://techtracer.com/2007/04/26/getting-started-with-ioc-a-simplified-tutorial/</guid>
		<description><![CDATA[I had mentioned about Inversion of Control (IoC) in my earlier post Inversion of Control &#8211; for easy integration. In this post I will show you how you can actually make your integration easy with implementation of a mini application sample based on IoC.
Objective
The main objective of this mini tutorial is to highlight the implementation [...]]]></description>
			<content:encoded><![CDATA[<p>I had mentioned about <strong>Inversion of Control</strong> (IoC) in my earlier post <a href="http://techtracer.com/2007/04/23/inversion-of-control-for-easy-integration/">Inversion of Control &#8211; for easy integration</a>. In this post I will show you how you can actually make your integration easy with implementation of a mini application sample based on <strong>IoC</strong>.</p>
<p><u><strong>Objective</strong></u><br />
The main objective of this mini tutorial is to highlight the implementation of an interface based Inversion of Control (IoC) architecture and to state how it could help in the integration and maintenance of the application.</p>
<p>For this mini application I will use the following set of classes</p>
<ol>
<li><strong>Dependency</strong> &#8211; This is the <em>interface</em> which is implementation as per my application versions progress and which will be used by the <strong>View</strong> components in my application. In other words this is my <strong>Model</strong> component in a <strong>MVC</strong> architecture</li>
<li><strong>Dependent</strong> &#8211; This is my <strong>View </strong>component which uses the <strong>Model</strong> component to produce the results for displaying. As the name suggests the <em>class</em> is dependent on the <strong>Dependency</strong> implementation.</li>
<li><strong>Injector</strong> &#8211; This is most important <em>class</em> in the application and which introduces the aspect of Inversion Of Control into the applications. This class can also be referred as the configurator class.</li>
<li>Finally there can a number of <strong>implementations</strong> of the <strong>Dependency</strong> interface according to the needs of the application.</li>
</ol>
<p><u><strong>Note:</strong></u> This mini tutorial is based on the concept of <strong>Interface based IOC</strong>. It also assumes that you import the required classed in your Classes when compiling them.</p>
<p>Lets start with our tutorial.</p>
<p><strong>Dependency.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Dependency <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Dependency is the generic interface which my application supports. The IoC is done by a <strong>configurater</strong> class which will inject the dependency of the implementation object based on the runtime requirements and then provide it to my application. Hence my application can support various types and versions of the implementations until and unless they follow the contract as per the <strong>Dependency</strong> interface. Sometimes this technique is referred to as &#8220;<strong>Design By Contract</strong>&#8220;. So for any interface based IoC design, your set of interfaces in the application must be finalized since they prove as the reference to be used inside the configurater classes.</p>
<p><strong>DependencyImplOne.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DependencyImplOne <span style="color: #000000; font-weight: bold;">implements</span> Dependency<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World! This is the first implementation for Dependency&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is my first implementation for the Dependency interface. It just implements the single method <em>doSomething() </em>and displays some text.</p>
<p><strong>ioc.properties</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"># You can have your own package instead of the one mentioned here.
package.name = com.techtracer.ioc
class.name = DependencyImplOne</pre></div></div>

<p>This is a simple external IoC<strong> configuration file </strong>. It defines the values for the package name and the implementation class name. I have used an external file in order to highlight the benefits of not having to hard-code the required parameters in the program.</p>
<p><strong>Injector.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Injector <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Dependency getInstance<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> propertiesFile<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Dependency d <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>	
&nbsp;
		<span style="color: #003399;">Properties</span> p <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Properties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			p.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span>propertiesFile<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not load file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003399;">String</span> packageName <span style="color: #339933;">=</span> p.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;package.name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> className <span style="color: #339933;">=</span> p.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;class.name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> impl <span style="color: #339933;">=</span> packageName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.&quot;</span> <span style="color: #339933;">+</span> className<span style="color: #339933;">;</span>		
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Instantiating Type : &quot;</span><span style="color: #339933;">+</span>  impl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			d <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Dependency<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span>impl<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">/*
			 * You have to remember to catch a Throwable here
			 * since only catching an Exception could still throw an
			 * NoClassDefFoundError if you supply a wrong class name
			 */</span>
			d <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> d<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally this is actual class on which my IoC architecture will depend. This is the <strong>configurator class</strong> which will inject the <strong>Dependency</strong> implemented object based on the application requirements in runtime. However I am free to change this class anytime I want and provide a new dependency to my application either through hard-coding or through some configuration <strong>properties or XML file</strong>. It is like a pre processor which acts between the object creation and the object consuming phase and is the heart of the IoC architecture.</p>
<p>You might see that I have used <strong>Reflection</strong> for getting the class name to be instantiated as shown in the syntax <em>Class.forName()</em>. You are not expected to know this and you can very well use a different type of instantiation method of your choice or simply create an <strong>new DependencyImplOne()</strong> and return it. Also for this tutorial I have used the above created <strong>ioc.properties</strong> file. Remember this file has to be stored in your application <strong>ROOT</strong> folder and not the package directories.</p>
<p><u><strong>Running the application </strong></u><br />
Now after creating all the classes you are ready to run the first IoC implementation. Just compile the set of classes in your package and after running the <strong>Dependent.java</strong> class you should be able to see the output:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">Instantiating Type : com.techtracer.ioc.DependencyImplOne
Hello World! This is the first implementation for Dependency</pre></div></div>

<p>Congratulations! if you were successfully able to run the program.</p>
<p><u><strong>IoC for easier integration</strong></u><br />
Now suppose at a later point of time you would like to change the behavior of your program and it should be able to produce another output instead of what the <strong>DependencyImplOne.java</strong> provides. But what if you have to do this without ever having to touch the source code of your earlier code. Is this possible?</p>
<p>The answer to the question is <strong>YES</strong>. This is because due to the IoC provided by the <strong>Injector</strong> class now you have the full flexibility in modifying the runtime behavior of your application by just creating another class which implements the Dependency interface and mention the newly created and compiled class inside your <strong>ioc.properties</strong> file.</p>
<p>So I would now create another class with the name <strong>DependencyImplTwo.java</strong> and simply put a different output String in place of the original.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DependencyImplTwo <span style="color: #000000; font-weight: bold;">implements</span> Dependency<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is the second implementation for Dependency&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Remember this class has to import the <strong>Dependency</strong> interface but it itself can be placed in any package regardless of the package structure of the original source code. After compiling the above class you should once again run the <strong>Dependent.java</strong> class as earlier and check the output. The output now should be:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">Instantiating Type : com.techtracer.ioc.DependencyImplTwo
This is the second implementation for Dependency</pre></div></div>

<p><u><strong>Conclusion</strong></u><br />
So, with this tutorial it is clear that with the help of an IoC architecture you can change the dependencies as and when required without affecting the rest of the components in your application, thus avoiding side effects of the frequent changes done to it. It&#8217;s also very easy to maintain and involves just placing the classes in their proper location.</p>
<p>Thus your application can be scaled and adapted to a changing environment by reducing the number of dependencies an application can encounter during its lifespan. However you have to be careful in designing the architecture and choose properly which are your <strong>Dependent</strong> components and  which are your <strong>Dependencies</strong> since the interfaces are used to map the dependencies with those required for the dependents.</p>
<h2>Related posts:</h2><ul><li><a href="http://techtracer.com/2008/11/24/reflection-in-java-simplified/" rel="bookmark" title="Permanent Link: Reflection in Java &#8211; Simplified">Reflection in Java &#8211; Simplified</a></li><li><a href="http://techtracer.com/2007/08/26/eclipse-europa-has-ruined-my-day/" rel="bookmark" title="Permanent Link: Eclipse Europa Has Ruined My Day">Eclipse Europa Has Ruined My Day</a></li><li><a href="http://techtracer.com/2007/08/19/working-with-jax-ws-in-jdk-16/" rel="bookmark" title="Permanent Link: Working With JAX-WS in JDK 1.6">Working With JAX-WS in JDK 1.6</a></li><li><a href="http://techtracer.com/about/" rel="bookmark" title="Permanent Link: About">About</a></li><li><a href="http://techtracer.com/2007/03/28/convert-date-to-string-and-string-to-date-in-java/" rel="bookmark" title="Permanent Link: Convert Date to String and String to Date in Java">Convert Date to String and String to Date in Java</a></li></ul><br /><a href="http://techtracer.com/">Techtracer.com</a> Copyright &copy; 2008<br /> ]]></content:encoded>
			<wfw:commentRss>http://techtracer.com/2007/04/26/getting-started-with-ioc-a-simplified-tutorial/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
