<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Chirag&#039;s Computer Blog</title>
	<atom:link href="https://techwithcj.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://techwithcj.wordpress.com</link>
	<description>A dose of my technical knowledge</description>
	<lastBuildDate>Sat, 15 Dec 2012 15:16:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='techwithcj.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>https://s0.wp.com/i/buttonw-com.png</url>
		<title>Chirag&#039;s Computer Blog</title>
		<link>https://techwithcj.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="https://techwithcj.wordpress.com/osd.xml" title="Chirag&#039;s Computer Blog" />
	<atom:link rel='hub' href='https://techwithcj.wordpress.com/?pushpress=hub'/>
	<item>
		<title>Blackberry Code Signing Hangs or Taking too long:  Java 7 Issue</title>
		<link>https://techwithcj.wordpress.com/2012/12/15/blackberry-code-signing-hangs-or-taking-too-long-java-7-issue/</link>
					<comments>https://techwithcj.wordpress.com/2012/12/15/blackberry-code-signing-hangs-or-taking-too-long-java-7-issue/#respond</comments>
		
		<dc:creator><![CDATA[Chirag Jain]]></dc:creator>
		<pubDate>Sat, 15 Dec 2012 15:15:20 +0000</pubDate>
				<category><![CDATA[Blackberry]]></category>
		<category><![CDATA[Blackberry Code Signing]]></category>
		<guid isPermaLink="false">http://techwithcj.wordpress.com/?p=206</guid>

					<description><![CDATA[I am working on a blackberry app for OS 7. I had received keys from Blackberry and tried to sign my app using Eclipse Signature tool in Blackberry Eclipse plugin. But whenever I started signing the app, it signed some of the .cod files and then hanged. Even after waiting for hours, it kept showing [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I am working on a blackberry app for OS 7. I had received keys from Blackberry and tried to sign my app using Eclipse Signature tool in Blackberry Eclipse plugin. But <strong>whenever I started signing the app, it signed some of the .cod files and then hanged</strong>. Even after waiting for hours, it kept showing &#8220;Receiving Response&#8221; for some .cod files, specially for RRT key file. After searching on net , I found that <strong>it was because of Java 7</strong>.</p>
<p>Though I have not referred Java 7 anywhere in Eclipse, it was using it when signing the code. because by default, my system path was set to Java 7, though Java 6 was also installed. <strong>I uninstalled Java 7 and then system was set to using Java 6 by default</strong>. After that, I signed the code and Wow! it was completed at the blink of the eye.</p>
<p>So Blackberry does not support Java 7 . Y<strong>ou need to install Java 6 and set its path in System Environment variables</strong> so that code signing process works well.</p>
<p>PS: My system is on Windows 7, 64 bit. I am developing app using Blackberry JRE 5.0.0</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techwithcj.wordpress.com/2012/12/15/blackberry-code-signing-hangs-or-taking-too-long-java-7-issue/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		
		<media:content url="https://0.gravatar.com/avatar/cef118fdd2068ee2fc84d477ae9f56a82e7aeb081d20c464fae72a98f3881603?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Chirag Jain</media:title>
		</media:content>
	</item>
		<item>
		<title>Difference in App and Web Server</title>
		<link>https://techwithcj.wordpress.com/2012/09/16/difference-in-app-and-web-server/</link>
					<comments>https://techwithcj.wordpress.com/2012/09/16/difference-in-app-and-web-server/#respond</comments>
		
		<dc:creator><![CDATA[Chirag Jain]]></dc:creator>
		<pubDate>Sun, 16 Sep 2012 13:23:01 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<guid isPermaLink="false">http://techwithcj.wordpress.com/?p=204</guid>

					<description><![CDATA[Here is a very good article describing difference between application server and web server. In another article, we can see what differentiates a full fledged Java EE application from a Java Web application.]]></description>
										<content:encoded><![CDATA[<p><a href="http://www.javaworld.com/javaqa/2002-08/01-qa-0823-appvswebserver.html" title="java Web and App server" target="_blank">Here</a> is a very good article describing difference between application server and web server.</p>
<p>In <a href="http://www.javaworld.com/javaworld/jw-01-2008/jw-01-tomcat6.html" title="Java EE and Web Application" target="_blank">another</a> article, we can see what differentiates a full fledged Java EE application from a Java Web application. </p>
]]></content:encoded>
					
					<wfw:commentRss>https://techwithcj.wordpress.com/2012/09/16/difference-in-app-and-web-server/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		
		<media:content url="https://0.gravatar.com/avatar/cef118fdd2068ee2fc84d477ae9f56a82e7aeb081d20c464fae72a98f3881603?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Chirag Jain</media:title>
		</media:content>
	</item>
		<item>
		<title>Method Overloading Ambiguity Problem</title>
		<link>https://techwithcj.wordpress.com/2012/05/18/method-overloading-ambiguity-problem/</link>
					<comments>https://techwithcj.wordpress.com/2012/05/18/method-overloading-ambiguity-problem/#respond</comments>
		
		<dc:creator><![CDATA[Chirag Jain]]></dc:creator>
		<pubDate>Fri, 18 May 2012 18:07:30 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Basics]]></category>
		<category><![CDATA[Overloading]]></category>
		<guid isPermaLink="false">http://techwithcj.wordpress.com/2012/05/18/while-going-thr-12/</guid>

					<description><![CDATA[While going through an article about method overriding in OOP, I stumbled upon an ambiguity problem in overriding. Here is the java code I wrote to test it: public class OverLoadingTest { static void testOverload(int i,double d) { System.out.println("testOverload 1: int, double"); } static void testOverload(double d,int i) { System.out.println("testOverload 2: double,int"); } public static [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>While going through an article about method overriding in OOP, I stumbled upon an ambiguity problem in overriding.</p>
<p>Here is the java code I wrote to test it:</p>
<p><code> public class OverLoadingTest { </code></p>
<p><code>static void testOverload(int i,double d) {</code><br />
<code>System.out.println("testOverload 1: int, double");</code><br />
<code>}</code></p>
<p><code>static void testOverload(double d,int i) {</code><br />
<code>System.out.println("testOverload 2: double,int");</code><br />
<code>}</code></p>
<p><code>public static void main(String[] args) {</code></p>
<p><code>testOverload(2,3.5);</code><br />
<code>testOverload(2.5,3);</code><br />
<code>testOverload(2,3); // Compilation error:&nbsp; The method testOverload(int, double) is ambiguous for the type OverLoadingTest</code><br />
 <code>&nbsp; }</code><br />
<code>}</code></p>
<p>In the third call testOverload(2,3), there will be a compilation error, because:</p>
<p><strong>First</strong> argument 2 is <strong>int</strong>, so java compiler will decide to call the method testOverload(<strong>int</strong>,double) because its first argument is int, so it is better match than .testOverload(double,int)</p>
<p><strong>Second</strong> argument 3 is also <strong>int</strong>, so java compiler will decide to call the method testOverload(double,<strong>int</strong>) because its second argument is int, so it is better match than testOverload(int,double)</p>
<p>As compiler will not be able to decide the right overloaded version for call, compilation error occurs.</p>
<p>Happy Coding !</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techwithcj.wordpress.com/2012/05/18/method-overloading-ambiguity-problem/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		
		<media:content url="https://0.gravatar.com/avatar/cef118fdd2068ee2fc84d477ae9f56a82e7aeb081d20c464fae72a98f3881603?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Chirag Jain</media:title>
		</media:content>
	</item>
		<item>
		<title>Enable/Disable Speakers/Headphones in Windows 7</title>
		<link>https://techwithcj.wordpress.com/2012/05/14/enabledisable-speakersheadphones-in-windows-7-10/</link>
					<comments>https://techwithcj.wordpress.com/2012/05/14/enabledisable-speakersheadphones-in-windows-7-10/#comments</comments>
		
		<dc:creator><![CDATA[Chirag Jain]]></dc:creator>
		<pubDate>Mon, 14 May 2012 17:20:08 +0000</pubDate>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Speakers]]></category>
		<category><![CDATA[Windows 7]]></category>
		<guid isPermaLink="false">http://techwithcj.wordpress.com/?p=106</guid>

					<description><![CDATA[Today I accidentally disabled my Windows 7 laptop speakers. I was installing Skype and testing it with headphones. I was getting the voice from other side from the laptop speakers, not through headphones. So I went to Control Panel&#8211;&#62; Sound and disabled the speakers. So now only headphones were enabled, so the voice started coming [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Today I accidentally disabled my Windows 7 laptop speakers. I was installing Skype and testing it with headphones. I was getting the voice from other side from the laptop speakers, not through headphones. So I went to Control Panel&#8211;&gt; Sound and disabled the speakers. So now only headphones were enabled, so the voice started coming through headphones. But now how to enable the speakers back? Now in Control Panel&#8211;&gt; Sound, I was not getting speakers anywhere, so there was no way to enable them. Then when I googled it and found <a href="http://www.itechtalk.com/thread4039.html">this</a> article.</p>
<p>The solution was simple. <strong>In Control Panel&#8211;&gt; Sound, in Playlist tab, Right Click and check Show Disabled Devices.</strong> Then select the disabled device, speakers in this case, go to Properties and there you can enable them. Hope this helps.</p>
<p>Happy Listening !</p>
<p>&nbsp;</p>
<p><a href="https://techwithcj.wordpress.com/wp-content/uploads/2012/05/enable-speakers-in-windows-71.png"><img data-attachment-id="115" data-permalink="https://techwithcj.wordpress.com/2012/05/14/enabledisable-speakersheadphones-in-windows-7-10/enable-speakers-in-windows-7-2/" data-orig-file="https://techwithcj.wordpress.com/wp-content/uploads/2012/05/enable-speakers-in-windows-71.png" data-orig-size="407,456" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}" data-image-title="enable speakers in windows 7" data-image-description="" data-image-caption="" data-medium-file="https://techwithcj.wordpress.com/wp-content/uploads/2012/05/enable-speakers-in-windows-71.png?w=268" data-large-file="https://techwithcj.wordpress.com/wp-content/uploads/2012/05/enable-speakers-in-windows-71.png?w=407" class="alignnone size-full wp-image-115" title="enable speakers in windows 7" src="https://techwithcj.wordpress.com/wp-content/uploads/2012/05/enable-speakers-in-windows-71.png?w=620" alt="enable speakers in windows 7"   srcset="https://techwithcj.wordpress.com/wp-content/uploads/2012/05/enable-speakers-in-windows-71.png 407w, https://techwithcj.wordpress.com/wp-content/uploads/2012/05/enable-speakers-in-windows-71.png?w=134&amp;h=150 134w, https://techwithcj.wordpress.com/wp-content/uploads/2012/05/enable-speakers-in-windows-71.png?w=268&amp;h=300 268w" sizes="(max-width: 407px) 100vw, 407px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://techwithcj.wordpress.com/2012/05/14/enabledisable-speakersheadphones-in-windows-7-10/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
		
		<media:content url="https://0.gravatar.com/avatar/cef118fdd2068ee2fc84d477ae9f56a82e7aeb081d20c464fae72a98f3881603?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Chirag Jain</media:title>
		</media:content>

		<media:content url="https://techwithcj.wordpress.com/wp-content/uploads/2012/05/enable-speakers-in-windows-71.png" medium="image">
			<media:title type="html">enable speakers in windows 7</media:title>
		</media:content>
	</item>
		<item>
		<title>toLowerCase() is Locale Based</title>
		<link>https://techwithcj.wordpress.com/2010/07/06/tolowercase-is-locale-based/</link>
					<comments>https://techwithcj.wordpress.com/2010/07/06/tolowercase-is-locale-based/#respond</comments>
		
		<dc:creator><![CDATA[Chirag Jain]]></dc:creator>
		<pubDate>Tue, 06 Jul 2010 12:51:21 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Strings]]></category>
		<category><![CDATA[upper lower case]]></category>
		<guid isPermaLink="false">http://techwithcj.wordpress.com/?p=84</guid>

					<description><![CDATA[I have found this very interesting and informative post for Java developers. Many Java developers may not know that toLowerCase() and toUpperCase() methods of java.lang.String class are Locale based. Have a look here at javadocs for these methods. toLowerCase() internally calls toLowerCase(Locale locale). Same is the case for toUpperCase().]]></description>
										<content:encoded><![CDATA[<p>I have found <a href="http://javapapers.com/core-java/javas-tolowercase-has-got-a-surprise-for-you/">this</a> very interesting and informative post for Java developers. Many Java developers may not know that <code>toLowerCase()</code> and <code>toUpperCase()</code> methods of java.lang.String class are Locale based. Have a look <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#toLowerCase%28%29">here</a> at javadocs for these methods.</p>
<p><code>toLowerCase()</code> internally calls <code>toLowerCase(Locale locale)</code>. Same is the case for <code>toUpperCase()</code>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techwithcj.wordpress.com/2010/07/06/tolowercase-is-locale-based/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		
		<media:content url="https://0.gravatar.com/avatar/cef118fdd2068ee2fc84d477ae9f56a82e7aeb081d20c464fae72a98f3881603?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Chirag Jain</media:title>
		</media:content>
	</item>
		<item>
		<title>Reading rss feed using java (JSP Taglib)</title>
		<link>https://techwithcj.wordpress.com/2010/05/13/reading-rss-feed-using-java-jsp-taglib/</link>
					<comments>https://techwithcj.wordpress.com/2010/05/13/reading-rss-feed-using-java-jsp-taglib/#comments</comments>
		
		<dc:creator><![CDATA[Chirag Jain]]></dc:creator>
		<pubDate>Thu, 13 May 2010 09:53:24 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java RSS]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[RSS]]></category>
		<guid isPermaLink="false">http://techwithcj.wordpress.com/?p=74</guid>

					<description><![CDATA[We can read RSS (Real Simple Syndication) feeds and publish them on our web page using JSP(Java Server Pages) tag library. Sun provides a rss taglib that can be used in JSP. It is extremely simple to use. You just need to have basic knowledge of JSP and HTML. So, how to do that? First [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>We can read RSS (Real Simple Syndication) feeds and publish them on our web page using JSP(Java Server Pages) tag library. Sun provides a rss taglib that can be used in JSP. It is extremely simple to use. You just need to have basic knowledge of JSP and  HTML.</p>
<p>So, how to do that? First you need to download the tag library file (rssutils.tld, rssutils.jar) from Sun&#8217;s website.<a href="http://java.sun.com/developer/technicalArticles/javaserverpages/rss_utilities/rss_utils_1.1.zip"> Here </a> is the url to download it. Put the files rssutils.tld and rssutils.jar in WEB-INF/lib directory of your web application.<br />
After that, choose a blog or website who publishes its rss feeds to users, and get the url of its rss feed. In below example, I have used the rss feed url of this blog. Please find the attached file <a href="https://techwithcj.wordpress.com/wp-content/uploads/2010/05/rsstest-jsp.doc"> here </a>and save it with .jsp extension (rssTest.jsp)<br />
You can use  tag also to read content in a loop.<br />
For a small tutorial on rss taglib, go  <a href="http://java.sun.com/developer/technicalArticles/javaserverpages/rss_utilities/">here </a></p>
<p>To know what RSS is, go  <a href="http://en.wikipedia.org/wiki/RSS">here </a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://techwithcj.wordpress.com/2010/05/13/reading-rss-feed-using-java-jsp-taglib/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		
		<media:content url="https://0.gravatar.com/avatar/cef118fdd2068ee2fc84d477ae9f56a82e7aeb081d20c464fae72a98f3881603?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Chirag Jain</media:title>
		</media:content>
	</item>
		<item>
		<title>enum as a class</title>
		<link>https://techwithcj.wordpress.com/2010/05/05/enum-as-a-class/</link>
					<comments>https://techwithcj.wordpress.com/2010/05/05/enum-as-a-class/#respond</comments>
		
		<dc:creator><![CDATA[Chirag Jain]]></dc:creator>
		<pubDate>Wed, 05 May 2010 11:42:25 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Enums]]></category>
		<category><![CDATA[Enum]]></category>
		<guid isPermaLink="false">http://techwithcj.wordpress.com/?p=72</guid>

					<description><![CDATA[Here I have compared enum with a class. In java, enums are like classes. What I have done here is, if we have to write a class that works like en enum, how can we do it. Though it is not of practicle use,what I have tried is to make the concept clear about enums.By [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Here I have compared enum with a class. In java, enums are like classes.  What I have done here is, if we have to write a class that works like en enum, how can we do it. Though it is not of practicle use,what I have tried is to make the concept clear about enums.By comparing enum with a class, we can understand how enums work.</p>
<p>Here DayEnum is a enum and DayClass is class for that enum.</p>
<p>enum DayEnum {<br />
<code>SUNDAY, MONDAY</code><br />
<code>}</code></p>
<p><code>class DayClass {</code><br />
<code>String value;</code><br />
<code>public DayClass (String str)</code><br />
<code>{</code><br />
<code>value=str;</code><br />
<code>}</code></p>
<p><code>static DayClass SUNDAY, MONDAY;</code><br />
<code>static</code><br />
<code>{</code><br />
<code>SUNDAY = new DayClass("SUNDAY");</code><br />
<code>MONDAY = new DayClass("MONDAY");</code><br />
<code>}</code><br />
<code>}</code></p>
<p><code>public class Test</code><br />
<code>{</code><br />
<code>public static void main(String args[])</code><br />
<code>{</code><br />
<code>DayClass dayClass = DayClass.SUNDAY;</code><br />
<code>DayEnum dayEnum = DayEnum.SUNDAY;</code><br />
<code>System.out.print("dayClass "+dayClass.value+" \n dayEnum "+dayEnum);</code><br />
<code>}</code><br />
<code>}</code></p>
<p>Here <code>SUNDAY, MONDAY</code> are similar to static objects of Day if Day were a class.</p>
<p>We use enum as:<br />
<code>DayEnum day = DayEnum.SUNDAY;</code></p>
<p>enums are like constants. To simulate that, I have used <em>value</em> in DayClass. So every object of DayClass has an integer value associated with it.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techwithcj.wordpress.com/2010/05/05/enum-as-a-class/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		
		<media:content url="https://0.gravatar.com/avatar/cef118fdd2068ee2fc84d477ae9f56a82e7aeb081d20c464fae72a98f3881603?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Chirag Jain</media:title>
		</media:content>
	</item>
		<item>
		<title>Java Good Practices</title>
		<link>https://techwithcj.wordpress.com/2010/02/12/java-good-practices/</link>
					<comments>https://techwithcj.wordpress.com/2010/02/12/java-good-practices/#comments</comments>
		
		<dc:creator><![CDATA[Chirag Jain]]></dc:creator>
		<pubDate>Fri, 12 Feb 2010 07:38:07 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Basics]]></category>
		<category><![CDATA[Java Exceptions]]></category>
		<category><![CDATA[Java Strings]]></category>
		<category><![CDATA[Good Practices]]></category>
		<guid isPermaLink="false">http://techwithcj.wordpress.com/?p=70</guid>

					<description><![CDATA[Here is an article on java good practices.Have a look!]]></description>
										<content:encoded><![CDATA[<p><a href="http://viralpatel.net/blogs/2010/02/most-useful-java-best-practice-quotes-java-developers.html">Here</a> is an article on java good practices.Have a look!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techwithcj.wordpress.com/2010/02/12/java-good-practices/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		
		<media:content url="https://0.gravatar.com/avatar/cef118fdd2068ee2fc84d477ae9f56a82e7aeb081d20c464fae72a98f3881603?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Chirag Jain</media:title>
		</media:content>
	</item>
		<item>
		<title>Private Fields of Reference Type</title>
		<link>https://techwithcj.wordpress.com/2010/02/12/private-fields-of-reference-type/</link>
					<comments>https://techwithcj.wordpress.com/2010/02/12/private-fields-of-reference-type/#comments</comments>
		
		<dc:creator><![CDATA[Chirag Jain]]></dc:creator>
		<pubDate>Fri, 12 Feb 2010 06:20:12 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Basics]]></category>
		<category><![CDATA[arraylist]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[private]]></category>
		<guid isPermaLink="false">http://techwithcj.wordpress.com/?p=68</guid>

					<description><![CDATA[Hi, today I stumbled upon a strange problem while wriring some java code. There is a class with a private field of reference type, say an ArrayList. Now,what is the use of private fields? They are used so that they can not be modified from outside the class like public fields. General practice is to [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Hi, today I stumbled upon a strange problem while wriring some java code.<br />
There is a class with a private field of reference type, say an ArrayList. Now,what is the use of private fields? They are used so that they can not be modified from outside the class like public fields. General practice is to provide getter methods in the class to return the values of private fields.It goes fine with the primitive type variables and Strings(because Strings are immutable). But when the field is of reference type, we can still change its value.Have a look at the code:</p>
<p><code>import java.util.*;</code></p>
<p><code>class Aclass</code><br />
<code>{</code></p>
<p><code>private ArrayList list;</code><br />
<code>private String s= "My string";</code></p>
<p><code>public Aclass()</code><br />
<code>{</code><br />
<code>list= new ArrayList();</code><br />
<code>list.add("Hello");</code><br />
<code>}</code></p>
<p><code>public ArrayList getList()</code><br />
<code>{</code><br />
<code>return list;</code><br />
<code>}</code></p>
<p><code>public String getString()</code><br />
<code>{</code><br />
<code>return s;</code><br />
<code>}</code><br />
<code>}</code></p>
<p><code>public class TestPrivate</code><br />
<code>{</code></p>
<p><code>public static void main(String args[])</code><br />
<code>{</code><br />
<code>Aclass ob= new a();</code><br />
<code>ArrayList alist= ob.getList();</code><br />
<code>System.out.println(l2);</code><br />
<code>alist.add("Hi");</code><br />
<code>ArrayList anotherList= ob.getList();</code><br />
<code>System.out.println(anotherList);</code></p>
<p><code>String s2= ob.getString();</code><br />
<code>System.out.println(s2);</code><br />
<code>s2="changed";</code><br />
<code>String s3= ob.getString();</code><br />
<code>System.out.println(s3);</code><br />
<code>}</code><br />
<code>}</code></p>
<p>When you run this code,the output is:<br />
<code>[Hello]</code><br />
<code>[Hello, Hi]</code><br />
<code>My string</code><br />
<code>My string</code></p>
<p>You can see that content of list has been changed, but for string, they are not changed.So you can change the list object even if it is private.It is because the <code>getList()</code> method returns the reference to the same list object, so we can modify the list even if it is private.Now if you think that it is some loophole or bug of java,the you are wrong.It is just a simple basic concept of reference type variables.</p>
<p>Now, what should you do to avoid this thing. You have to modify the <code>getList()</code> method so that it returns the clone of list, not the actual list object.Here is the solution:</p>
<p><code>public ArrayList getList()</code><br />
<code>{</code><br />
<code>return (ArrayList) list.clone();</code><br />
<code>}</code></p>
<p><code>clone()</code> will return the clone of object, not the actual list. Return type  of clone() is Object, so we have to typecast it to <code>ArrayList</code>. Any modification done from the reference returned by this method will not change the actual one.For more information on clone() method,<br />
<a href="https://techwithcj.wordpress.com/2010/01/22/cloning-of-objects-using-clone-in-java/">go here.</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://techwithcj.wordpress.com/2010/02/12/private-fields-of-reference-type/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		
		<media:content url="https://0.gravatar.com/avatar/cef118fdd2068ee2fc84d477ae9f56a82e7aeb081d20c464fae72a98f3881603?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Chirag Jain</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem With clone() if Object Has a Reference Type Attribute</title>
		<link>https://techwithcj.wordpress.com/2010/01/22/problem-with-clone-if-object-has-a-reference-type-attribute/</link>
					<comments>https://techwithcj.wordpress.com/2010/01/22/problem-with-clone-if-object-has-a-reference-type-attribute/#respond</comments>
		
		<dc:creator><![CDATA[Chirag Jain]]></dc:creator>
		<pubDate>Fri, 22 Jan 2010 13:01:30 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Basics]]></category>
		<category><![CDATA[clone]]></category>
		<guid isPermaLink="false">http://techwithcj.wordpress.com/?p=63</guid>

					<description><![CDATA[You might have used the clone() method of Object class to get a clone of an object.But have you ever faced a problem if your object has reference type attributes? Well, I have, and thats what I am going to discuss in this post. A class can have two types of class level variables,primitive type [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>You might have used the clone() method of Object class to <a href="https://techwithcj.wordpress.com/2010/01/22/cloning-of-objects-using-clone-in-java/">get a clone</a> of an object.But have you ever faced a problem if your object has reference type attributes? Well, I have, and thats what I am going to discuss in this post.<br />
A class can have two types of class level variables,primitive type (as int,float) and reference type(as List).Here is an example:</p>
<p><code>class AClass </code><br />
<code>{</code><br />
<code>int i;        //primitive type</code><br />
<code>List alist;  //reference type</code><br />
<code>}</code><br />
For more on types,<a href="http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html"> click here</a></p>
<p>Well, lets return on the original topic.When we make a clone of an object, the primitive type variables are copied in the new clone object, but for the reference type variables,only the reference is copied, not the original instance.For example, if we create the clone of above class&#8217;s object,there will be only one List object, and both object will have a reference to this.Here is what I tried:</p>
<p><code>public class ClassToClone  {</code><br />
<code>    ArrayList list;</code><br />
<code>   public ClassToClone(ArrayList list)</code><br />
<code>    {</code><br />
<code>        this.list=list;</code><br />
<code>    }</code></p>
<p><code>    protected Object clone() </code><br />
<code>    {</code><br />
<code>        try{</code><br />
<code>               return super.clone();</code><br />
<code>        }</code><br />
<code>        catch(CloneNotSupportedException e)</code><br />
<code>        {</code><br />
<code>            return null;</code><br />
<code>        }</code><br />
<code>    }</code><br />
<code>}</code></p>
<p>To test it:<br />
<code>public class TestClone  {</code><br />
<code>    public static void main(String args[])</code><br />
<code>    {</code><br />
<code>        ClassToClone c1,c2;</code><br />
<code>        ArrayList arraylist= new ArrayList();</code><br />
<code>        arraylist.add("Hello");</code><br />
<code>        c1= new ClassToClone(arraylist);</code><br />
<code>        c2 = (ClassToClone)c1.clone();</code><br />
<code>        c1.list.add("Hi"); //change contents of the list of c1</code><br />
<code>        System.out.print(c1.list.toString());</code><br />
<code>        System.out.print(c2.list.toString()); // prints the list of both objects</code><br />
<code>    }</code><br />
<code>}</code></p>
<p>When you run this program, you will see that on changing the list of c1, list of c2 is also changed and they both have the same content.<br />
To avoid this problem, you have to explicitly clone the reference type variables. In ClassToClone, replace clone() method by this:</p>
<p><code>protected Object clone() </code><br />
<code>    {</code><br />
<code>        try{</code><br />
<code>            ClassToClone aobj = (ClassToClone)super.clone();</code><br />
<code>                            aobj.s = (ArrayList)list.clone(); </code><br />
<code>                                             //explicit call to clone() for arraylist</code><br />
<code>                            return aobj;</code></p>
<p><code>        }</code><br />
<code>        catch(CloneNotSupportedException e)</code><br />
<code>        {</code><br />
<code>            return null;</code><br />
<code>        }</code><br />
<code>    }</code></p>
<p>Here we have explicitly cloned the arraylist.Now a new list will be created for clone object.Now changes done in c1.list do not reflect in c2.list.You can try this by yourself.But remember,<strong>clone() can be called only if the class implements Cloneable interface</strong>, otherwise we get <code>CloneNotSupportedException</code>.Here class ArrayList implements this interface.<br />
<a href="http://java.sun.com/developer/JDCTechTips/2001/tt0306.html">Here</a> and <a href="http://en.wikipedia.org/wiki/Clone_%28Java_method%29">here</a> is a great article to read on object cloning.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techwithcj.wordpress.com/2010/01/22/problem-with-clone-if-object-has-a-reference-type-attribute/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		
		<media:content url="https://0.gravatar.com/avatar/cef118fdd2068ee2fc84d477ae9f56a82e7aeb081d20c464fae72a98f3881603?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Chirag Jain</media:title>
		</media:content>
	</item>
	</channel>
</rss>
