<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6529523707014588436</id><updated>2012-01-10T21:21:52.283-08:00</updated><category term='package'/><category term='starters'/><category term='hashcode'/><category term='events'/><category term='EnvInfoProvider'/><category term='JSF 2'/><category term='RIA'/><category term='DefaultSessioncookieFactory'/><category term='insert'/><category term='library'/><category term='MetaInfo'/><category term='JDeveloper'/><category term='Text'/><category term='inheritance'/><category term='popup'/><category term='IOS'/><category term='drawbacks'/><category term='message'/><category term='setters'/><category term='implementing Cloneable'/><category term='integrated'/><category term='ViewObject'/><category term='bind'/><category term='same methods'/><category term='toplink'/><category term='difference'/><category term='abstract'/><category term='config.xml'/><category term='lifecycle'/><category term='jsf'/><category term='java'/><category term='attribute'/><category term='advantages'/><category term='example'/><category term='efficient'/><category term='best practices'/><category term='pros'/><category term='Design'/><category term='default settings'/><category term='deep cloning'/><category term='getters'/><category term='batch'/><category term='timezone'/><category term='status bar'/><category term='android'/><category term='Datasource'/><category term='== operator'/><category term='jpa'/><category term='session'/><category term='OOP'/><category term='weblogic'/><category term='configurations'/><category term='java archive'/><category term='notification'/><category term='object comparison'/><category term='rules'/><category term='executing'/><category term='benefits'/><category term='jdbc'/><category term='javascript'/><category term='Button'/><category term='immediate'/><category term='overriding clone method'/><category term='classpath'/><category term='Objective C'/><category term='executable'/><category term='interface'/><category term='class per hierarchy'/><category term='Clone'/><category term='Group'/><category term='EntityObject'/><category term='developers'/><category term='redirect page'/><category term='invalid'/><category term='compilation'/><category term='optimized'/><category term='class'/><category term='first level of cache'/><category term='Rectangle'/><category term='Cloneable interface'/><category term='polymorphism'/><category term='Java Virtual Machine'/><category term='programmatically'/><category term='sessionless'/><category term='encapsulation'/><category term='inputtext'/><category term='readonly'/><category term='shallow cloning'/><category term='recommendations'/><category term='memory leak'/><category term='hibernate'/><category term='JVM'/><category term='Custom'/><category term='navigation'/><category term='starter'/><category term='overriding equals and hashCode'/><category term='cons'/><category term='programming'/><category term='Controls'/><category term='manifest'/><category term='-cp'/><category term='program'/><category term='port number'/><category term='internal'/><category term='persistence unit'/><category term='object literal'/><category term='commandbutton'/><category term='loading'/><category term='network adapter'/><category term='RegionSite'/><category term='equality check'/><category term='ADF'/><category term='JavaFX'/><category term='IllegalStateException'/><category term='begginer'/><category term='running'/><category term='viewcontroller'/><category term='CloneNotSupportedException'/><category term='17002'/><category term='structure'/><category term='source code'/><category term='compiling'/><category term='page fragments'/><category term='model'/><category term='equals'/><category term='Things to consider while cloning of objects'/><category term='session cache'/><title type='text'>Java Solutions</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>28</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-8843223811397472080</id><published>2011-11-14T03:33:00.001-08:00</published><updated>2011-11-19T03:12:50.965-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='invalid'/><category scheme='http://www.blogger.com/atom/ns#' term='page fragments'/><category scheme='http://www.blogger.com/atom/ns#' term='IllegalStateException'/><category scheme='http://www.blogger.com/atom/ns#' term='ADF'/><category scheme='http://www.blogger.com/atom/ns#' term='RegionSite'/><title type='text'>Common ADF Mistakes - Conversion pages to page fragments</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Suppose you have developed ADF task flow with pages (not page fragments) and later on you decide that these pages should be page fragments, and you convert that taskflow to support page fragments. After conversion, if you get this error&lt;br /&gt;&lt;div&gt;&lt;blockquote class="tr_bq"&gt;java.lang.IllegalStateException: Attempt to validate an already invalid RegionSite:&lt;/blockquote&gt;&lt;/div&gt;&lt;div&gt;Check whether you have used PageController for page or not. If you used then convert it to RegionController. ADF does not give you indication of this. If you want to debug error, do the following:&lt;/div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;Goto bindings of page on which you dropped taskflow containing page fragments.&lt;/li&gt;&lt;li&gt;In Executables, click the taskflow and set its property "activation" to "immediate"&lt;/li&gt;&lt;li&gt;Now run and you will get detailed error.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;u&gt;Update&lt;/u&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;&lt;div&gt;It can also occur if your taskflow is calling any method before page loading and that method is throwing some error.&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-8843223811397472080?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/8843223811397472080/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=8843223811397472080' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8843223811397472080'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8843223811397472080'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2011/11/common-adf-mistakes.html' title='Common ADF Mistakes - Conversion pages to page fragments'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-4267939712948475511</id><published>2011-03-13T10:04:00.000-07:00</published><updated>2011-03-13T10:04:33.268-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='efficient'/><category scheme='http://www.blogger.com/atom/ns#' term='optimized'/><category scheme='http://www.blogger.com/atom/ns#' term='internal'/><category scheme='http://www.blogger.com/atom/ns#' term='redirect page'/><category scheme='http://www.blogger.com/atom/ns#' term='JSF 2'/><title type='text'>JSF 2 Internals - Efficient way of Redirecting to a Page</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;There are certain cases where you are needed to redirect &amp;nbsp;to a different page than requested by user e.g. if user has requested a secure page for which he/she is not authorized, your application will have to redirect to login page or some other page. I have created a utility method to redirect to a page. You can call anywhere but for example given above, you can call it a beforePhase method of RestoreViewPhaseListener. Here is the method code:&lt;br /&gt;&lt;code&gt;    &lt;style type="text/css"&gt;p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #4b9073}span.s1 {color: #941067}span.s2 {color: #000000}span.s3 {text-decoration: underline}span.Apple-tab-span {white-space:pre}&lt;/style&gt;   &lt;/code&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="s1"&gt;public&lt;/span&gt; &lt;span class="s1"&gt;static&lt;/span&gt; &lt;span class="s1"&gt;void&lt;/span&gt; redirect(String viewId) {&lt;/code&gt;&lt;/div&gt;&lt;code&gt; &lt;/code&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;execute(FacesContext.getCurrentInstance(), viewId);&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;}&lt;/code&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="s1"&gt;private&lt;/span&gt; &lt;span class="s1"&gt;static&lt;/span&gt; &lt;span class="s1"&gt;void&lt;/span&gt; execute(FacesContext facesContext, String viewId)&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="s1"&gt;throws&lt;/span&gt; FacesException {&lt;/code&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;UIViewRoot viewRoot = &lt;span class="s1"&gt;null&lt;/span&gt;;&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;ViewDeclarationLanguage vdl = facesContext.getApplication()&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;.getViewHandler()&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;.getViewDeclarationLanguage(facesContext, viewId);&lt;/code&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="s1"&gt;if&lt;/span&gt; (vdl != &lt;span class="s1"&gt;null&lt;/span&gt;) {&lt;/code&gt;&lt;/div&gt;&lt;div class="p3"&gt;&lt;code&gt;&lt;span class="s2"&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;/span&gt;// If we have one, get the ViewMetadata...&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;ViewMetadata metadata = vdl.getViewMetadata(facesContext,&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;viewId);&lt;/code&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="p3"&gt;&lt;code&gt;&lt;span class="s2"&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="s1"&gt;if&lt;/span&gt;&lt;span class="s2"&gt; (metadata != &lt;/span&gt;&lt;span class="s1"&gt;null&lt;/span&gt;&lt;span class="s2"&gt;) { &lt;/span&gt;// perhaps it's not supported&lt;/code&gt;&lt;/div&gt;&lt;div class="p3"&gt;&lt;code&gt;&lt;span class="s2"&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;/span&gt;// and use it to create the ViewRoot. This will have, at&lt;/code&gt;&lt;/div&gt;&lt;div class="p3"&gt;&lt;code&gt;&lt;span class="s2"&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;/span&gt;// most&lt;/code&gt;&lt;/div&gt;&lt;div class="p3"&gt;&lt;code&gt;&lt;span class="s2"&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;/span&gt;// the UIViewRoot and its &lt;span class="s3"&gt;metadata&lt;/span&gt; facet.&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;viewRoot = metadata.createMetadataView(facesContext);&lt;/code&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;}&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;}&lt;/code&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;facesContext.setViewRoot(viewRoot);&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;facesContext.renderResponse();&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="s1"&gt;assert&lt;/span&gt; (&lt;span class="s1"&gt;null&lt;/span&gt; != viewRoot);&lt;/code&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;code&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;}&lt;/code&gt;&lt;/div&gt;&lt;code&gt;&lt;/code&gt;&lt;br /&gt;viewId is the name of page to which it should redirect e.g. "/redirect.xml" (assuming page is at root level). Inside execute method, you get the ViewDeclarationLanguage which is page type specific e.g. if page is in facelet markup, you get DefaultFaceletViewDeclarationLanguage and if page is in JSP markup, then you will get corresponding ViewDeclarationLanguage. After this, page root tree is constructed by calling createMetadataView if it is not already constructed. Finally set the new ViewRoot and call renderResponse method, which will bypass all phases and will jump directly to render phase.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-4267939712948475511?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/4267939712948475511/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=4267939712948475511' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/4267939712948475511'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/4267939712948475511'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2011/03/jsf-2-internals-efficient-way-of.html' title='JSF 2 Internals - Efficient way of Redirecting to a Page'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-1069038154262611135</id><published>2011-03-08T00:18:00.000-08:00</published><updated>2011-03-08T00:18:30.758-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='loading'/><category scheme='http://www.blogger.com/atom/ns#' term='source code'/><category scheme='http://www.blogger.com/atom/ns#' term='JSF 2'/><category scheme='http://www.blogger.com/atom/ns#' term='configurations'/><title type='text'>JSF 2 Internals - Configurations</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;In JSF 2, there is almost no need to mention any configuration in a small application and defaults values are sufficient enough to fulfill the purpose. All the default configurations are in com.sun.faces.WebConfiguration. As i mentioned in my previous blog &lt;a href="http://javainnovations.blogspot.com/2011/03/jsf-2-internals-entry-of-facesservlet.html"&gt;post&lt;/a&gt;, JSF has implemented ServletContainerInitializer which is new interface in Servlet 3.0 in order to register servlets and other stuff programmatically at the time of loading application. In 'onStartUp' method, it adds listener com.sun.faces.ConfigureListener. It implements various interface listeners like ServletRequestListener, HttpSessionListener, ServletContextListener, ServletRequestAttributeListener, HttpSessionAttributeListener, ServletContextAttributeListener. Most important for loading configuration is ServletCotextListeenr which has two methods contextInitialized and contextDestroyed. contextInitialized &amp;nbsp;is called when application loads and ServletContext is created, so this is the best time to load configurations in JEE web applications. JSF 2 also gets benefit of this and creates WebConfiguration Object and loads default values for configurations. If application has its own values mentioned in web.xml, they are overriden.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-1069038154262611135?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/1069038154262611135/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=1069038154262611135' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/1069038154262611135'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/1069038154262611135'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2011/03/jsf-2-internals-configurations.html' title='JSF 2 Internals - Configurations'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-1721381486135461900</id><published>2011-03-02T04:57:00.000-08:00</published><updated>2011-03-02T05:10:55.334-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='source code'/><category scheme='http://www.blogger.com/atom/ns#' term='default settings'/><category scheme='http://www.blogger.com/atom/ns#' term='JSF 2'/><title type='text'>JSF 2 Internals - Entry of FacesServlet is optional</title><content type='html'>In JEE 6, major milestone achieved was reduction of configuration files e.g. in servlet based web application, no need to mention web.xml and application assumes default configuration settings. Same thing was also achieved in JSF 2. Now there is no need to mention FaceServlet or servlet mapping in web.xml. But question arises from where application picks these defaults? Answer is the  "ServletContainerInitializer" interface which allows a library/runtime to be notified of a web application's startup phase and perform any required programmatic registration of servlets,filters, and listeners in response to it. Inside of "onStartup" implementation of JSF 2, all these defaults are set, Sample code is given below:&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  &gt;&lt;pre&gt;&lt;code&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  &gt;&lt;div&gt; ServletRegistration reg =&lt;/div&gt;&lt;div&gt;                  servletContext.addServlet("FacesServlet",&lt;/div&gt;&lt;div&gt;                                            "javax.faces.webapp.FacesServlet");&lt;/div&gt;&lt;div&gt;            reg.addMapping("/faces/*", "*.jsf", "*.faces");&lt;/div&gt;&lt;div&gt;            servletContext.setAttribute(RIConstants.FACES_INITIALIZER_MAPPINGS_ADDED, Boolean.TRUE);&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  &gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  &gt;JSF 2 class which implements this interface is "FacesInitializer" class and have been mentioned under jsf-ri.jar's META-INF/services folder.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  &gt;&lt;br /&gt;&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; font-size: medium; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; font-size: medium; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-1721381486135461900?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/1721381486135461900/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=1721381486135461900' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/1721381486135461900'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/1721381486135461900'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2011/03/jsf-2-internals-entry-of-facesservlet.html' title='JSF 2 Internals - Entry of FacesServlet is optional'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-8342062143491244446</id><published>2011-02-24T00:27:00.000-08:00</published><updated>2011-02-24T04:26:23.042-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Objective C'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='IOS'/><category scheme='http://www.blogger.com/atom/ns#' term='setters'/><category scheme='http://www.blogger.com/atom/ns#' term='memory leak'/><category scheme='http://www.blogger.com/atom/ns#' term='encapsulation'/><category scheme='http://www.blogger.com/atom/ns#' term='getters'/><title type='text'>Learning IOS Programming for Java Developers - Memory Management in Setters and Getters Of Object</title><content type='html'>&lt;div&gt;Encapsulation is one of the basic building block in Object Oriented paradigm. Encapsulation in Java is achieve by restricting user to directly access the fields of Object and in order to access those fields, setters and getters are written. Objective C is also an object oriented programming language and also provides encapsulation by providing setters and getters to access the object data. Here is the sample class which contains two fields. One is an object and second one is of basic data type.&lt;/div&gt;&lt;div&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;@interface TestClass : NSObject&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;{&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;NSString *name;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;int&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt; id;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;}&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;- (void) setName : (NSString *) newName;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;- (NSString *) name;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;- (void) setId : (int) newId;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;- (int) id;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;"setName" and "name" is setter and getter of "name" field. Similarly "setId" and "id" id setter and getter of "id" field.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Here is the implementation part of class&lt;/div&gt;&lt;div&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;@implementation TestClass&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;{&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;- (void) setName : (NSString *) newName&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;{&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;[newName retain];&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;[name release];&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;name = newName;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;- (NSString *) name&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;{&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;return name;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;-&lt;/span&gt; (void) setId : (int) newId&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;{&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;id = newId;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;- (int) id&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;{&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;return id;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  &gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;code&gt;}&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  &gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;setName needs your special intention for memory management point of view. First you send message "retain" to parameter, then you release old field by sending message "release" to name pointer. Then you assign new value to name pointer. These three steps are critical because if you change the order like you release old one and then retain new parameter, if both are same, then by sending release message will free the memory and new parameter will also be freed because that was same as old one. By sending retain message to parameter, you actually gain ownership of it and its reference count increases. Since you have also ownership of old one, so you will have to release it otherwise you will lose reference and there will be memory leak. Now as a java developer i can think that if "name" field is null, then sending release message may give something like NullPointerException but in Objective C, sending a message to null is safe and it will not throw any error and will keep executing next line.&lt;/div&gt;&lt;div&gt;Now last thing which is missing is that how to release fields which are retained before releasing object memroy, so for that you will have to overwrite a special method of NSObject "dealloc". This method is called when you send release message to an object and its reference count is 0. In this method you will have to release all those objects which you have retained. Sample implementation for above class is given below:&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;pre&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;- (void) dealloc&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;{&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;[name release];&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;[super dealloc];&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-8342062143491244446?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/8342062143491244446/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=8342062143491244446' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8342062143491244446'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8342062143491244446'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2011/02/learning-ios-programming-for-java.html' title='Learning IOS Programming for Java Developers - Memory Management in Setters and Getters Of Object'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-7479004518084700216</id><published>2011-02-23T06:08:00.000-08:00</published><updated>2011-02-23T06:14:46.929-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Objective C'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='IOS'/><category scheme='http://www.blogger.com/atom/ns#' term='developers'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='starters'/><title type='text'>How to Learn IOS Programming having experience In Java language</title><content type='html'>I am creating this post to help those which are working in java programming language and want to explore or work in IOS. I am working in Java for last eight years and now exploring IOS programming so will share my experiences through this post. &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;First phase is to refresh knolwedge of C and then learn syntax of Objective C because Objective C is superset of C. I found this book very helpful for this phase &lt;a href="http://www.amazon.com/Learning-Objective-C-2-0-Hands--Developers/dp/0321711386/ref=sr_1_8?ie=UTF8&amp;amp;qid=1298470315&amp;amp;sr=8-8"&gt;Learning Objective-C 2.0: A Hands-On Guide to Objective-C for Mac and IOS Developers&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-7479004518084700216?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/7479004518084700216/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=7479004518084700216' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/7479004518084700216'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/7479004518084700216'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2011/02/how-to-learn-ios-programming-having.html' title='How to Learn IOS Programming having experience In Java language'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-1152083764143497817</id><published>2011-02-07T22:29:00.000-08:00</published><updated>2011-02-08T04:07:58.610-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='programmatically'/><category scheme='http://www.blogger.com/atom/ns#' term='Datasource'/><category scheme='http://www.blogger.com/atom/ns#' term='ADF'/><category scheme='http://www.blogger.com/atom/ns#' term='EnvInfoProvider'/><category scheme='http://www.blogger.com/atom/ns#' term='jdbc'/><category scheme='http://www.blogger.com/atom/ns#' term='DefaultSessioncookieFactory'/><title type='text'>Setting datasource name programmatically for ADF Application</title><content type='html'>While working on an enterprise application for my client, i received a request from the client that data source name should not be hardcoded in the application and it should be configurable because client wants to deploy it more than one instance of application on the same server for different purposes like preview, production etc. So i googled it, and found excellent article &lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;&lt;a href="http://blogs.oracle.com/smuenchadf/examples/#129"&gt;11g Dynamic JDBC Credentials for Model 1, Struts, Trinidad, and ADF Faces Rich Client&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;Here it is simplified version:&lt;br /&gt;You will have to extend two following classes:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;EnvInfoProvider&lt;/li&gt;&lt;li&gt;DefaultSessionCookieFactory&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;EnvInfoProvider&lt;/span&gt;&lt;br /&gt;This is main interface and datasource name will be set in the implementation of this interace. You will have to implement following three methods:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;public Object getInfo(String propName, Object object)&lt;/li&gt;&lt;li&gt;public void modifyInitialContext(Object initialContext)&lt;/li&gt;&lt;li&gt;public int getNumOfRetries()&lt;/li&gt;&lt;/ul&gt;Only important method for our scenario is getInfo method. It is used to set new datasource name. Sample implementation is given below:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;div&gt;public Object getInfo(String propName, Object object) {&lt;br /&gt;&lt;div&gt;&lt;div&gt;        String myDataSourceName = "test"; // You can set it through your logic&lt;/div&gt;&lt;div&gt;        if (object instanceof Hashtable) {&lt;/div&gt;&lt;div&gt;            Hashtable connectionEnv = (Hashtable)object;&lt;/div&gt;&lt;div&gt;          System.out.println(connectionEnv.get(Configuration.JDBC_CONNECTION_NAME));&lt;/div&gt;&lt;div&gt;          String contextPath = (String)ADFContext.getCurrent().getSessionScope().get("contextPath");&lt;/div&gt;&lt;div&gt;              connectionEnv.put(Configuration.JDBC_DS_NAME,"java:comp/env/jdbc/"+myDataSourceName );&lt;/div&gt;&lt;div&gt;        }&lt;/div&gt;&lt;div&gt;        return null;&lt;/div&gt;&lt;div&gt;    }&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;    public void modifyInitialContext(Object initialContext) {&lt;/div&gt;&lt;div&gt;    }&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;    public int getNumOfRetries() {&lt;/div&gt;&lt;div&gt;        return 0;&lt;/div&gt;&lt;div&gt;    }&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;DefaultSessionCookieFactory&lt;/b&gt;&lt;/div&gt;&lt;div&gt;In order to configure custom implementation of EnvInfoProvider, you will have to extend DefaultSessionCookieFactory and override createSessionCookie method. Sample implementation is given below:&lt;/div&gt;&lt;div&gt;&lt;code&gt;&lt;pre&gt;&lt;/div&gt;&lt;div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;  public SessionCookie createSessionCookie(String name,&lt;/div&gt;&lt;div&gt;                                           String value,&lt;/div&gt;&lt;div&gt;                                           ApplicationPool pool, &lt;/div&gt;&lt;div&gt;                                           Properties properties) {&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;    SessionCookie cookie = super.createSessionCookie(name, value, pool, properties);&lt;/div&gt;&lt;div&gt;    Hashtable env = pool.getEnvironment();&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;    env.remove(Configuration.JDBC_CONNECTION_NAME);&lt;/div&gt;&lt;div&gt;    EnvInfoProvider provider = new MyEnvInfoProvider();&lt;/div&gt;&lt;div&gt;    cookie.setEnvInfoProvider(provider);&lt;/div&gt;&lt;div&gt;    return cookie; &lt;/div&gt;&lt;div&gt;  } &lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;Now last step is to configure this custom SessionCookieFactory. Open bc4j.xcfg of application module and add following element as a child of respected AppModuleConfig element&lt;/div&gt;&lt;div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;am-pooling maxpoolsize="30" sessioncookiefactoryclass="MyDefaultSessionCookiedFactory"&gt;&lt;/am-pooling&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-1152083764143497817?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/1152083764143497817/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=1152083764143497817' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/1152083764143497817'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/1152083764143497817'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2011/02/setting-datasource-name.html' title='Setting datasource name programmatically for ADF Application'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-3184842668980387509</id><published>2010-07-29T12:08:00.000-07:00</published><updated>2010-07-29T12:35:16.445-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MetaInfo'/><category scheme='http://www.blogger.com/atom/ns#' term='structure'/><category scheme='http://www.blogger.com/atom/ns#' term='ViewObject'/><category scheme='http://www.blogger.com/atom/ns#' term='EntityObject'/><category scheme='http://www.blogger.com/atom/ns#' term='ADF'/><category scheme='http://www.blogger.com/atom/ns#' term='readonly'/><title type='text'>Accessing Runtime metadata for ViewObject and EntityObject</title><content type='html'>&lt;div style="text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;In Oracle ADF, ViewObject and EntityObject are two basic concepts and whole application revolves around these two type of objects. EntityObject corresponds to database table while ViewObject corresponds to Database View. ViewObject can be based on EntityObject, SQL or on transient attributes. Normally you create and design these two objects throug drag and drop in JDeveloper but sometimes you need some meta information about these two objects in business logic. In order to get metainfo, some understanding of underlying structure of these two objects should be. &lt;div&gt;&lt;img src="http://4.bp.blogspot.com/_JaPG-95CW88/TFHVUMZkwLI/AAAAAAAAAoI/0Z6afpUSMgA/s320/Structure_VO_EO.png" style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 193px;" border="0" alt="" id="BLOGGER_PHOTO_ID_5499411162898677938" /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:Tahoma, sans-serif;font-size:small;"&gt;Figure shown above illustrates the three primary interfaces ADF provides for accessing runtime metadata about view objects and entity objects&lt;a id="sthref2913" name="sthref2913"&gt;&lt;/a&gt;&lt;a id="sthref2914" name="sthref2914"&gt;&lt;/a&gt;. The &lt;code style="font-family: monospace; font-size: 12px; "&gt;ViewObject&lt;/code&gt; interface extends the &lt;code style="font-family: monospace; font-size: 12px; "&gt;StructureDef&lt;/code&gt; interface. The class representing the entity definition (&lt;code style="font-family: monospace; font-size: 12px; "&gt;EntityDefImpl&lt;/code&gt;) also implements this interface. As its name implies, the &lt;code style="font-family: monospace; font-size: 12px; "&gt;StructureDef&lt;/code&gt; defines the structure and the component and provides access to a collection of &lt;code style="font-family: monospace; font-size: 12px; "&gt;AttributeDef &lt;/code&gt;objects that offer runtime metadata about each attribute in the view object row or entity row. Using an &lt;code style="font-family: monospace; font-size: 12px; "&gt;AttributeDef&lt;/code&gt;, you can access its companion &lt;code style="font-family: monospace; font-size: 12px; "&gt;AttributeHints&lt;/code&gt; object to reference hints like the display label, format mask, tooltip, etc.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:Tahoma, sans-serif;font-size:small;"&gt;Now you can use this understanding to get runtime meta info about view object or entity object e.g. if i want to find whether a ViewObject is readonly non entity based object or not. i can find it by:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:Tahoma, sans-serif;font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:Tahoma, sans-serif;font-size:small;"&gt;&lt;span class="Apple-style-span"  style=" ;font-size:small;"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;&lt;code style="font-family: monospace; font-size: 12px; "&gt;isFullSql()&lt;/code&gt; is &lt;code style="font-family: monospace; font-size: 12px; "&gt;true&lt;/code&gt;&lt;/p&gt;&lt;p&gt;This method returns true if the view object's SQL query is completely specified by the developer, as opposed to having the select list derived automatically based on the participating entity usages.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;code style="font-family: monospace; font-size: 12px; "&gt;getEntityDefs()&lt;/code&gt; is &lt;code style="font-family: monospace; font-size: 12px; "&gt;null&lt;/code&gt;&lt;/p&gt;&lt;p&gt;This method returns an array of &lt;code style="font-family: monospace; font-size: 12px; "&gt;EntityDefImpl&lt;/code&gt; objects representing the view object's entity usages. If it returns &lt;code style="font-family: monospace; font-size: 12px; "&gt;null&lt;/code&gt;, then the view object has no entity usages.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:Tahoma, sans-serif;font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:Tahoma, sans-serif;font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:Tahoma, sans-serif;font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-3184842668980387509?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/3184842668980387509/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=3184842668980387509' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/3184842668980387509'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/3184842668980387509'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2010/07/accessing-runtime-metadata-for.html' title='Accessing Runtime metadata for ViewObject and EntityObject'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_JaPG-95CW88/TFHVUMZkwLI/AAAAAAAAAoI/0Z6afpUSMgA/s72-c/Structure_VO_EO.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-8624544182489308110</id><published>2010-05-19T22:34:00.000-07:00</published><updated>2010-05-19T22:42:14.862-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='config.xml'/><category scheme='http://www.blogger.com/atom/ns#' term='integrated'/><category scheme='http://www.blogger.com/atom/ns#' term='JDeveloper'/><category scheme='http://www.blogger.com/atom/ns#' term='weblogic'/><title type='text'>Integrated Weblogic Server Domain</title><content type='html'>I was developing an application on JDeveloper 11g and deploying it on intergrated weblogic server. In my application i was authenticating using my own authentication provider but when i configured it on weblogic, weblogic failed to start. I was not finding any configuration file where i can revert security settings of weblogic but after some try i found it and thought to share it with others. When you install JDeveloper on windows, it places configuration files under "C:\Documents and Settings\user_name\Application Data\JDeveloper". Here you can find all settings for jdeveloper and integrated weblogic. Under this directory there is a folder with something "system11.....". Now to change weblogic related settings, open "defaultDomain/config.xml" and change it accordingly.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-8624544182489308110?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/8624544182489308110/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=8624544182489308110' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8624544182489308110'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8624544182489308110'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2010/05/integrated-weblogic-server-domain.html' title='Integrated Weblogic Server Domain'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-7451795922978821896</id><published>2009-12-29T07:50:00.000-08:00</published><updated>2009-12-29T07:51:55.641-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='programmatically'/><category scheme='http://www.blogger.com/atom/ns#' term='ADF'/><category scheme='http://www.blogger.com/atom/ns#' term='jsf'/><category scheme='http://www.blogger.com/atom/ns#' term='navigation'/><title type='text'>Programmatically Navigating to another page in adf / jsf</title><content type='html'>Here is the sample code which can be used to programmatically navigating to another page in jsf or adf&lt;br /&gt;&lt;br /&gt;FacesContext fctx = FacesContext.getCurrentInstance();&lt;br /&gt;Application application = fctx.getApplication();&lt;br /&gt;NavigationHandler navHandler = application.getNavigationHandler();&lt;br /&gt;navHandler.handleNavigation(fctx,null, "name of navigation case");&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-7451795922978821896?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/7451795922978821896/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=7451795922978821896' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/7451795922978821896'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/7451795922978821896'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2009/12/programmatically-navigating-to-another.html' title='Programmatically Navigating to another page in adf / jsf'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-3690252112461667481</id><published>2009-12-29T07:42:00.000-08:00</published><updated>2009-12-29T07:48:12.345-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='programmatically'/><category scheme='http://www.blogger.com/atom/ns#' term='popup'/><category scheme='http://www.blogger.com/atom/ns#' term='ADF'/><title type='text'>Doing some work before showing Popup in ADF</title><content type='html'>Here is the way to do any processing before calling popup. First of all create a method in backing bean and assign this to action of "commandButton" or "commandLink". Sample signature for you method is as follows:&lt;br /&gt;&lt;br /&gt;public void myMethod()&lt;br /&gt;    {&lt;br /&gt;        /**&lt;br /&gt;         * Place your processing logic here and then call showPoup method&lt;br /&gt;         **/&lt;br /&gt;        showPopup(popupId);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;Signature of showPopup is:&lt;br /&gt;&lt;br /&gt;public static void showPopup(String popupId)&lt;br /&gt;    {&lt;br /&gt;        FacesContext context = FacesContext.getCurrentInstance();&lt;br /&gt; &lt;br /&gt;        ExtendedRenderKitService extRenderKitSrvc =&lt;br /&gt;            Service.getRenderKitService(context, ExtendedRenderKitService.class);&lt;br /&gt;        extRenderKitSrvc.addScript(context,&lt;br /&gt;                                   "AdfPage.PAGE.findComponent('" + popupId + "').show();");&lt;br /&gt;    }&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-3690252112461667481?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/3690252112461667481/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=3690252112461667481' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/3690252112461667481'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/3690252112461667481'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2009/12/doing-some-work-before-showing-popup-in.html' title='Doing some work before showing Popup in ADF'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-9137441060727199255</id><published>2009-12-25T06:59:00.000-08:00</published><updated>2009-12-25T07:08:46.709-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='commandbutton'/><category scheme='http://www.blogger.com/atom/ns#' term='immediate'/><category scheme='http://www.blogger.com/atom/ns#' term='lifecycle'/><category scheme='http://www.blogger.com/atom/ns#' term='inputtext'/><category scheme='http://www.blogger.com/atom/ns#' term='jsf'/><title type='text'>JSF Life cycle Tip # 1</title><content type='html'>Usually people think that by setting the attribute "immediate" true, bypasses the validation life cycle but this is not true in every case. If you talk about CommandButton and CommandLink, then its true and after "Apply Request Values" phase, request processing cycle shifts to "Render Response" phase thus skipping "Process Validation","Update Model Values", and "Invoke Application". ActionListener and action method associcated with CommandButton / CommandLink is executed in "Apply Request Values".&lt;br /&gt;When we talk about editableValueHolder like InputText, and other controls, setting immediate "true" does not bypass the validation or other phases. Infact it just prioritize the control for validation e.g. if you want to validate one input control first than others, then you can set "immediate" for that control to "true".&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-9137441060727199255?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/9137441060727199255/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=9137441060727199255' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/9137441060727199255'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/9137441060727199255'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2009/12/jsf-life-cycle-tip-1.html' title='JSF Life cycle Tip # 1'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-4530316159147301230</id><published>2009-10-28T22:46:00.000-07:00</published><updated>2009-10-28T23:05:29.217-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='starter'/><category scheme='http://www.blogger.com/atom/ns#' term='pros'/><category scheme='http://www.blogger.com/atom/ns#' term='begginer'/><category scheme='http://www.blogger.com/atom/ns#' term='JDeveloper'/><category scheme='http://www.blogger.com/atom/ns#' term='cons'/><category scheme='http://www.blogger.com/atom/ns#' term='ADF'/><title type='text'>Pros and Cons of JDeveloper 11g and ADF from begginers Point of View</title><content type='html'>I have started to use JDeveloper 11g and ADF for one of my enterprise level project. I didn't use any part of ADF before. Its now one month and i want to share with world what i found in ADF with JDeveloper 11g.&lt;br /&gt;&lt;br /&gt;Pros: &lt;br /&gt;- It is based on MVC pattern and cleanly separates your business logic from presentation layer.&lt;br /&gt;- It has built in support for AJAX&lt;br /&gt;- Very rapid development because most of the development one can do declaratively by just drag and drop components on pages and like that&lt;br /&gt;- Most important and exciting feature from me was very good support of LOV. LOV is required in almost every business application and with ADF i don't have to do any code for LOV just a query and some very small configurations and that's it.&lt;br /&gt;- Rich Tables supporting LOV, combo box and others. &lt;br /&gt;- It provides good hooks to develop own framework or utility classes to suit development e.g. I want to extend all entities from my own class and i can do it very easily.&lt;br /&gt;- One of the unique feature of ADF 11g is ADF Task flow which faciliates developer to define reusable navigation and avoid cluttering of all navigation rules in one file which happens in JSF&lt;br /&gt;&lt;br /&gt;Cons&lt;br /&gt;Now we discuss cons of ADF and JDeveloper 11g&lt;br /&gt;- JDeveloper is buggy and crashes many times espeically when one does refactoring or deleting some resources&lt;br /&gt;- You will have to spend good time to learn ADF if you didn't have prior experience with Oracle technologies&lt;br /&gt;- Query component in ADF Faces which is used to build criteria does not have option to change the layout of fields like grouping etc.&lt;br /&gt;- LOV which are generated are also buggy. I am still facing one problem and no one still gave me solution for that.&lt;br /&gt;- You will have to do lot of hit and trials to understand the behaviour of layout components&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-4530316159147301230?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/4530316159147301230/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=4530316159147301230' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/4530316159147301230'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/4530316159147301230'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2009/10/pros-and-cons-of-jdeveloper-11g-and-adf.html' title='Pros and Cons of JDeveloper 11g and ADF from begginers Point of View'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-453527590780009426</id><published>2009-10-08T04:04:00.000-07:00</published><updated>2009-10-08T04:08:25.962-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='timezone'/><category scheme='http://www.blogger.com/atom/ns#' term='viewcontroller'/><category scheme='http://www.blogger.com/atom/ns#' term='model'/><category scheme='http://www.blogger.com/atom/ns#' term='ADF'/><title type='text'>ADF Error timezone region not found</title><content type='html'>When i deploy/run adf applications, i receive error:&lt;br /&gt;&lt;pre&gt;timezone region  not found&lt;/pre&gt;&lt;br /&gt;In order to solve this, right click on Model project and select Project properties. In "Run/Debug/Profile" select "Edit" in "Run Configurations" and add "-Duser.timezone=UTC" in JavaOptions. Repeat same steps for ViewController Project.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-453527590780009426?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/453527590780009426/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=453527590780009426' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/453527590780009426'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/453527590780009426'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2009/10/adf-error-timezone-region-not-found.html' title='ADF Error timezone region not found'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-5532222032990088018</id><published>2009-10-06T01:21:00.000-07:00</published><updated>2009-10-06T01:28:18.670-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JDeveloper'/><category scheme='http://www.blogger.com/atom/ns#' term='JVM'/><category scheme='http://www.blogger.com/atom/ns#' term='Java Virtual Machine'/><title type='text'>JDeveloper11g Startup error</title><content type='html'>I installed JDeveloper11g Studio Edition with no JDK bundle but when i tried to start JDeveloper, it gave me error&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;Unable to start Java Virtual Machine located at ....&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;In order to fix this type of error, go to installation_directoy\Middleware\JDeveloper\jdev\bin folder&lt;br /&gt;and edit file jdev.conf. &lt;br /&gt;Locate line "AddVMOption -XX:MaxPermSize=256M". &lt;br /&gt;Now add another line just below this line:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;AddVMOption -Xmx256M&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-5532222032990088018?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/5532222032990088018/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=5532222032990088018' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/5532222032990088018'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/5532222032990088018'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2009/10/jdeveloper11g-startup-error.html' title='JDeveloper11g Startup error'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-4781040704930344324</id><published>2009-10-03T07:55:00.000-07:00</published><updated>2009-10-03T08:42:06.137-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='jpa'/><category scheme='http://www.blogger.com/atom/ns#' term='persistence unit'/><category scheme='http://www.blogger.com/atom/ns#' term='toplink'/><title type='text'>Persistence Unit for Toplink Essentials</title><content type='html'>Minimum configurations required for persistence unit in case of Toplink essentials is given below:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&amp;lt;?xml version="1.0" encoding="windows-1252" ?&amp;gt;&lt;br /&gt;&amp;lt;persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&lt;br /&gt;             version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"&amp;gt;&lt;br /&gt;  &amp;lt;persistence-unit name="Model" transaction-type="RESOURCE_LOCAL"&amp;gt;&lt;br /&gt;    &amp;lt;provider&amp;gt;&lt;br /&gt;        oracle.toplink.essentials.PersistenceProvider&lt;br /&gt;    &amp;lt;/provider&amp;gt;&lt;br /&gt;    &amp;lt;class&amp;gt;org.engineers.adf.hrdemo.model.Departments&amp;lt;/class&amp;gt;&lt;br /&gt;    &amp;lt;class&amp;gt;org.engineers.adf.hrdemo.model.Employees&amp;lt;/class&amp;gt;&lt;br /&gt;    &amp;lt;properties&amp;gt;&lt;br /&gt;        &amp;lt;property name="toplink.logging.level" value="INFO"/&amp;gt;&lt;br /&gt;        &amp;lt;property name="toplink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/&amp;gt;&lt;br /&gt;        &amp;lt;property name="toplink.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:XE"/&amp;gt;&lt;br /&gt;        &amp;lt;property name="toplink.jdbc.password" value="password"/&amp;gt;&lt;br /&gt;        &amp;lt;property name="toplink.jdbc.user" value="user"/&amp;gt;&lt;br /&gt;    &amp;lt;/properties&amp;gt;&lt;br /&gt; &lt;br /&gt;  &amp;lt;/persistence-unit&amp;gt;&lt;br /&gt;  &lt;br /&gt;&amp;lt;/persistence&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-4781040704930344324?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/4781040704930344324/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=4781040704930344324' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/4781040704930344324'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/4781040704930344324'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2009/10/persistence-unit-for-toplink-essentials.html' title='Persistence Unit for Toplink Essentials'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-7608351673670573420</id><published>2009-10-03T07:26:00.000-07:00</published><updated>2009-10-03T07:53:53.493-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='port number'/><category scheme='http://www.blogger.com/atom/ns#' term='jpa'/><category scheme='http://www.blogger.com/atom/ns#' term='17002'/><category scheme='http://www.blogger.com/atom/ns#' term='toplink'/><category scheme='http://www.blogger.com/atom/ns#' term='network adapter'/><title type='text'>JPA with Toplink Essentials Error Code: 17002</title><content type='html'>From this post, I am going to start to post solution for different problems or errors in java technologies which i face during development so that others also could get help from my research and don't waste time on that now.&lt;br /&gt;&lt;br /&gt;Error is given below:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.1 (Build b52-fcs (09/24/2008))):&lt;br /&gt;oracle.toplink.essentials.exceptions.DatabaseException&lt;br /&gt;Internal Exception: java.sql.SQLException: Io exception:&lt;br /&gt;Invalid number format for port number&lt;br /&gt;Error Code: 17002&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cause Was: I just copied url from a web site and then pasted to persistence. Probably it appended some hidden characters due to which provider game an error about port format. I just rewrite the port number again and error resolved.&lt;br /&gt;&lt;br /&gt;Second Error which i faced was:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.1 (Build b52-fcs (09/24/2008))):&lt;br /&gt;oracle.toplink.essentials.exceptions.DatabaseException&lt;br /&gt;Internal Exception: java.sql.SQLException:&lt;br /&gt;Io exception: The Network Adapter could not establish the connection&lt;br /&gt;Error Code: 17002&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cause Was: wrong port number, ip address or network disconnection&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-7608351673670573420?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/7608351673670573420/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=7608351673670573420' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/7608351673670573420'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/7608351673670573420'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2009/10/jpa-with-toplink-essentials-error-code.html' title='JPA with Toplink Essentials Error Code: 17002'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-7876357090994021294</id><published>2009-05-10T09:24:00.000-07:00</published><updated>2009-05-10T10:52:51.213-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='notification'/><category scheme='http://www.blogger.com/atom/ns#' term='status bar'/><category scheme='http://www.blogger.com/atom/ns#' term='message'/><category scheme='http://www.blogger.com/atom/ns#' term='android'/><title type='text'>How to Send Message to Status Bar in Android</title><content type='html'>Status bar is very important part of android interface. It alerts the user about new sms, emails and many other stuff. Applications can also send messages or notifications to status bar for user. In this post, I will guide the user how to send messages to status bar using a simple application.&lt;br /&gt;Application has a simple interface which has a edit box and a button. User will type any text in edit box and will click on button. which will send the message to status bar. Given below is the code for interface:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color:#ff9900;"&gt;&lt;em&gt;package org.engineers.android;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import android.app.Notification;&lt;br /&gt;import android.app.NotificationManager;&lt;br /&gt;import android.app.PendingIntent;&lt;br /&gt;import android.content.Context;&lt;br /&gt;import android.content.Intent;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.view.View;&lt;br /&gt;import android.view.View.OnClickListener;&lt;br /&gt;import android.view.ViewGroup.LayoutParams;&lt;br /&gt;import android.widget.Button;&lt;br /&gt;import android.widget.EditText;&lt;br /&gt;import android.widget.LinearLayout;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt;*&lt;br /&gt;* @author amer sohail&lt;br /&gt;*/&lt;br /&gt;public class NotificationActivity extends Activity {&lt;br /&gt;&lt;br /&gt;private EditText editor;&lt;br /&gt;&lt;br /&gt;/** Called when the activity is first created. */&lt;br /&gt;@Override&lt;br /&gt;public void onCreate(Bundle icicle) {&lt;br /&gt;super.onCreate(icicle);&lt;br /&gt;// ToDo add your GUI initialization code here&lt;br /&gt;setContentView(getEditor());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private LinearLayout getEditor(){&lt;br /&gt;LinearLayout parent = new LinearLayout(this);&lt;br /&gt;parent.setOrientation(LinearLayout.VERTICAL);&lt;br /&gt;editor = new EditText(this);&lt;br /&gt;LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.5f);&lt;br /&gt;parent.addView(editor ,lp);&lt;br /&gt;&lt;br /&gt;LinearLayout actionBar = new LinearLayout(this);&lt;br /&gt;actionBar.setOrientation(LinearLayout.HORIZONTAL);&lt;br /&gt;Button sendButton = new Button(this);&lt;br /&gt;sendButton.setText("Send");&lt;br /&gt;sendButton.setOnClickListener(listener);&lt;br /&gt;actionBar.addView(sendButton);&lt;br /&gt;parent.addView(actionBar,lp);&lt;br /&gt;return parent;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private OnClickListener listener = new OnClickListener() {&lt;br /&gt;&lt;br /&gt;public void onClick(View arg0) { &lt;/em&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;em&gt;&lt;span style="color:#ff9900;"&gt;//write code here&lt;br /&gt;}&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/em&gt;&lt;/code&gt;&lt;br /&gt;&lt;span style="color:#333333;"&gt;Above code is very simple. There is an activity named as "NotificationActivity" extended from Activity class. This will be the view of application. Inside "onCreate" method, content view is set which is developed in separate method "getEditor". Although good approach to write interface is in xml but i wrote it here using code because this was a very simple application. I used Linear Layout with vertical orientation as main layout. It contains EditBox and Button vertically. A OnClickListener was also set to button's OnClickListener. OnClickListener contains a method onClick where i will write main code to sent message to status bar. Code for that is given below:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;em&gt;&lt;br /&gt;&lt;span style="color:#ff9900;"&gt;NotificationManager manager= (NotificationManager)NotificationActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);&lt;br /&gt;Notification notification = new Notification(R.drawable.alert_dialog_icon,null, System.currentTimeMillis());&lt;br /&gt;PendingIntent contentIntent = PendingIntent.getActivity(NotificationActivity.this, 0,&lt;br /&gt;new Intent(NotificationActivity.this, NotificationActivity.class), 0);&lt;br /&gt;notification.setLatestEventInfo(NotificationActivity.this, "Alert", editor.getText(), contentIntent);&lt;br /&gt;manager.notify(1, notification);&lt;br /&gt;&lt;/span&gt;&lt;/em&gt;&lt;/code&gt;&lt;br /&gt;&lt;span style="color:#333333;"&gt;NotificationManager is main class which is used to send messages in the form of Notification to the status bar. NotificationManager is gotten from Content.getSystemService method. Since Acitivity is extended from Context class, so i used "NotificationActivity" to get NotificationManager. Next step is to create a Notification. It has many constructors but constructor which i have used contains three parameters. First one is "icon" to be displayed in the notification. second one is message which i will set later and third one is time in milliseconds of generating the message. Notification object also requires a PendingIntent to display acitivity when user will click on message in status bar. PendingIntent.getActivity is used to create PendingIntent for activity. In the last, i call notify of NotificationManager which takes two parameter, first one is unique identifier for notification and second one is Notification itself. That's it. You can send notification to status bar in four simple lines. Create a project and try it. I will post another blog to explain PendingIntent in details.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-7876357090994021294?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/7876357090994021294/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=7876357090994021294' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/7876357090994021294'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/7876357090994021294'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2009/05/how-to-send-message-to-status-bar-in.html' title='How to Send Message to Status Bar in Android'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-8274494595895495089</id><published>2008-10-02T09:22:00.000-07:00</published><updated>2008-10-20T05:56:02.312-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='object literal'/><category scheme='http://www.blogger.com/atom/ns#' term='Group'/><category scheme='http://www.blogger.com/atom/ns#' term='JavaFX'/><category scheme='http://www.blogger.com/atom/ns#' term='Custom'/><category scheme='http://www.blogger.com/atom/ns#' term='attribute'/><category scheme='http://www.blogger.com/atom/ns#' term='Controls'/><category scheme='http://www.blogger.com/atom/ns#' term='events'/><category scheme='http://www.blogger.com/atom/ns#' term='Design'/><category scheme='http://www.blogger.com/atom/ns#' term='Button'/><category scheme='http://www.blogger.com/atom/ns#' term='Text'/><category scheme='http://www.blogger.com/atom/ns#' term='RIA'/><category scheme='http://www.blogger.com/atom/ns#' term='bind'/><category scheme='http://www.blogger.com/atom/ns#' term='Rectangle'/><title type='text'>Creating Custom Button in JavaFX</title><content type='html'>&lt;div&gt;JavaFX is an exciting new technology to create RIA for different platforms and devices like desktop, web, mobile, tv etc. JavaFX provides a scripting language and API to create rich widgets, and user interfaces.&lt;br /&gt;In this episode of blog, i will guide you how to create a custom button in JavaFX shown below. I also like to thanks Mr. Asim Rasool which guided me to choose color combinations. He is graphic designer and working for last five years as Graphic Expert.&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_JaPG-95CW88/SOT4fEu3u4I/AAAAAAAAADo/59y1XU4qNmw/s1600-h/button.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5252596278150806402" style="margin: 0px auto 10px; display: block; cursor: pointer; text-align: center;" alt="" src="http://2.bp.blogspot.com/_JaPG-95CW88/SOT4fEu3u4I/AAAAAAAAADo/59y1XU4qNmw/s320/button.jpg" border="0" /&gt;&lt;/a&gt; First we create a Button class which extends from CustomNode class. In JavaFX, each UI Element is a Node. In order to design our own UI Nodes in JavaFX, one has to extend CustomNode.&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;code&gt;&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public class Button extends CustomNode&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Now if we think about features a button should provide to user then we come across width, height, x and y coordinates, and text which will appear on button. So we add following attributes to Button class.&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;code&gt;&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;/span&gt;&lt;pre&gt;&lt;span style="color:#ff9900;"&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public class Button extends CustomNode&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{ &lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;    attribute x:Number;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;    attribute y:Number;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;    attribute width:Number;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;    attribute height:Number;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;    attribute text:String;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;    private attribute txt:Text;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;/span&gt; &lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;    private attribute gradient:LinearGradient = LinearGradient {&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;        startX: 0.0        startY: 0.0&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;        endX: 0.0        endY: 1.0&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;        proportional: true&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;        stops: [&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;            Stop { offset: 0.0 color: Color.web("#dceaff")},&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;            Stop { offset: 0.49 color: Color.web("#6885b2") },&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;            Stop { offset: 0.5 color: Color.web("#2c599c") },&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;            Stop { offset: 1.0 color: Color.web("#bed3f4") }&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;        ]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;   }&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;In JavaFX, attribute is same as property in Java. Note that , there is a private attribute "txt" of type "Text" which is a node and used to draw text. In addition to txt, there is another private attribute which is gradient. I have used LinearGradient which is used to add gradient to node. In this case i have initialized LinearGradient using Object Literal notation. LinearGradient contains startX,startY, endX, and endY which specifies the area in which gradient should be applied. In this case, i want to apply gradient from top to bottom of button, so i mentioned startY as 0.0(top) and endY as 1.0(bottom). proportional attribute establishes smooth transition from one color to another color. As we know that gradient is a transition from one color to other, in order to mention those colors, stops array is used. Each Stop has two important attributes one is "offset" and second one is "color". offset is used to indicate where a particular color should be applied in full intensity. In my case it is  from startX and startY.&lt;br /&gt;&lt;br /&gt;Each class which extends from CustomNode, have to implement "create" function which returns Node object.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;public function create():Node{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        return Group{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            translateX:x translateY:y;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            content:[Rectangle{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                width:width height:height&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                arcWidth:35 arcHeight:35&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                stroke:Color.web("#023493");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                strokeWidth:1.5;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                fill: bind gradient&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                onMouseEntered: function( e: MouseEvent ):Void {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    gradient = LinearGradient {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    startX: 0.0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    startY: 0.0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    endX: 0.0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    endY: 1.0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    proportional: true&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    stops: [&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        Stop { offset: 0.0 color: Color.web("#ffffff")},&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        Stop { offset: 0.49 color: Color.web("#6885b2") },&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        Stop { offset: 0.5 color: Color.web("#2c599c") },&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        Stop { offset: 1.0 color: Color.web("#ffffff") }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        ]&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                onMouseExited: function( e: MouseEvent ):Void {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    gradient = LinearGradient {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        startX: 0.0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        startY: 0.0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        endX: 0.0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        endY: 1.0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        proportional: true&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        stops: [&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                            Stop { offset: 0.0 color: Color.web("#dceaff")},&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                            Stop { offset: 0.49 color: Color.web("#6885b2") },&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                            Stop { offset: 0.5 color: Color.web("#2c599c") },&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                            Stop { offset: 1.0 color: Color.web("#bed3f4") }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                        ]&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            },getText()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        ]&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        onMousePressed: function( e: MouseEvent ):Void {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;           txt.x = txt.x + 1;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;           txt.y = txt.y + 1;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        onMouseReleased: function( e: MouseEvent ):Void {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;           txt.x = txt.x - 1;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;           txt.y = txt.y - 1;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In create function, i have returned Group which is a node used to combine two or more nodes in a single node. I have encapsulated a Rectangle node and Text node inside a Group node. Benefit of using Group will be that x, y coordinates of Rectangle and Text will be from left upper corner of Group, so just changing Group's coordinates, i can relocate Rectangle and Text automatically.&lt;br /&gt;I have assigned gradient to fill attribute of Rectangle to produce gradient effect. I also used "bind" keyword which means, whenever gradient object will be changed, fill attribute of Rectangle will be recalculated. You can note it that i have changed gradient object on mouse events of Rectangle like onMouseEntered and onMouseExited. Other attributes are self explanatory. Text node is obtained through private function "getText".&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;private function getText():Text{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        txt = Text{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                content:text&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                fill:Color.WHITE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                font:Font{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    name:"Arial" size:14 style:FontStyle.BOLD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                effect:DropShadow{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    color:Color.BLACK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    offsetX:1 offsetY:1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;           centralizeText(txt);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;           &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;         return txt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    private function centralizeText(text:Text):Void{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;         text.x = (width - text.getWidth())/2;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;         text.y = (height - text.getHeight())/2 + 12;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;         &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;if you look at the getText function carefully, you will notice use of another function centralizeText, this function gets a Text node and sets its x and y on the basis of current width and height of text to appear on center of Rectangle. Best approach to centralize text is to get the text width in font and then adjust it but i don't find any such approach in JavaFX, so i have to get use of "Text" node's widht and height attributes which is i think not a good approach.&lt;br /&gt;That's all now. You have created Custom Button successfully. Let's use it now. Code to use it given below:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;Frame {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    title: "Custom Button"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    width: 220&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    height: 160&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    closeAction: function() { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        java.lang.System.exit( 0 ); &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    visible: true&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    stage: Stage {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        content: [&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            Button{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                x:20&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                y:20&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                width:120&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                height:30&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                text:"Submit"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                onMouseClicked: function( e: MouseEvent ):Void {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    System.out.println("This button has been clicked");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        ]&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So, in this blog we explored following key concepts like:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Use of Object literal&lt;/li&gt;&lt;li&gt;Use of Group Node&lt;/li&gt;&lt;li&gt;How to implement events&lt;/li&gt;&lt;li&gt;How to create its own Node&lt;/li&gt;&lt;/ul&gt;In my coming blog i will create more complex nodes so stay in touch.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-8274494595895495089?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/8274494595895495089/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=8274494595895495089' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8274494595895495089'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8274494595895495089'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2008/10/creating-custom-button-in-javafx.html' title='Creating Custom Button in JavaFX'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_JaPG-95CW88/SOT4fEu3u4I/AAAAAAAAADo/59y1XU4qNmw/s72-c/button.jpg' height='72' width='72'/><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-2294294418076233827</id><published>2008-08-19T01:17:00.000-07:00</published><updated>2008-09-22T01:28:03.420-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='rules'/><category scheme='http://www.blogger.com/atom/ns#' term='same methods'/><category scheme='http://www.blogger.com/atom/ns#' term='class'/><category scheme='http://www.blogger.com/atom/ns#' term='benefits'/><category scheme='http://www.blogger.com/atom/ns#' term='interface'/><category scheme='http://www.blogger.com/atom/ns#' term='difference'/><category scheme='http://www.blogger.com/atom/ns#' term='abstract'/><title type='text'>Abstract Classes and Interfaces</title><content type='html'>&lt;div&gt;In java, Abstract classes and Interfaces are two building blocks which play an important role nearly in every java program. &lt;/div&gt;&lt;div&gt;Abstract classes are classes which can not be instantiated. It may or may not contain one or more than one methods which have only signature but don't have any body/definition. The methods which don't have body, are called as abstract methods. In order to use abstract classes, one has to extend it and provide definition for all abstract methods. If body of abstract method is not provided in extension class, that child class will also be abstract. Classes are declared abstract with keyword "abstract". Methods are also declared abstract using "abstract" keyword. &lt;/div&gt;&lt;code&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public abstract class Shape&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;public abstract void draw();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;/span&gt; &lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public class Rectangle extends Shape&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public void draw()&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;//implementation for rectangle&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;/span&gt; &lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public class Triangle extends Shape&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public void draw()&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;//implementation for triangle&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/code&gt;&lt;br /&gt;&lt;div&gt;In the above example, Shape is an abstract class which contains one abstact method draw. Each class which extends from Shape like Rectangle and Triangle have to provide its own particular implementation of draw method and that is logical because drawing nature of each Shape is different and each Shape should know how to draw itself. So abstract class Shape provides a bond for all geometry figures that if they are Shape, then they have to provide definition of draw method.&lt;/div&gt;&lt;br /&gt;&lt;div&gt; &lt;/div&gt;&lt;div&gt;Interfaces are same as Abstract classes in the sense that it contains only those methods which have only signatures but don't have any body. In order to declare interface, "interface" keyword is used before the name of interface. Since interface contains only abstract methods, so "abstract" keyword is not used in method's signature for interface.&lt;/div&gt;&lt;code&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public interface Shape&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public void draw();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;/span&gt; &lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public class Rectangle implements Shape&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public void draw()&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;//implementation for Rectangle&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;/span&gt; &lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public class Triangle implements Shape&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public void draw()&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;//implementation for Triangle&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/code&gt;&lt;br /&gt;&lt;div&gt;In the above example, an interface "Shape" has been declared which contains one method signature "draw". All those classes which want to have functionality of Shape, implements it and provide body of draw method. &lt;/div&gt;&lt;br /&gt;&lt;div&gt;In above examples, i achieved same functionality through abstract class and interface. Now suppose we want to display these shapes on Canvas. For that Canvas class provides a method to add those Shapes and then there is show method of Canvas which internally calls draw method of each Shape added on Canvas as shown below:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0); font-family: -webkit-monospace; font-size: 13px; "&gt;public class Canvas&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;code&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;private List&lt;shape&gt; shapes = new ArrayList&lt;shape&gt;();&lt;/shape&gt;&lt;/shape&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public void add(Shape shape)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;shapes.add(shape);&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public void show()&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;for (Shape s : shapes)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;s.draw();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;}      &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public class Main&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public static void main(String args[])&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Canvas canvas = new Canvas();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Shape rec1 = new Rectangle();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;canvas.add(rec1);&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Shape tri1 = new Triangle();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;canvas.add(tri1);&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;canvas.show();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/code&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;You can see that Canvas class is usable whether you use Abstract class or interface. Now question arises in which situation one should go for Abstract class and other should go for interface. Suppose above Canvas is used to display both Shapes and Widgets and Canvas does not have any knowledge whether a particular item added on it, is Shape or Widget. Since Widget are not shapes so natural choice in this case is interface approach. There should be an interface like Drawable which should be implemented by both Shapes and Widget. Structure of Shape, Widget, and Canvas is shown below:&lt;div&gt;&lt;code&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;&lt;pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;public abstract class Shape implements Drawable&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;public void draw();&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;//implementation of Rectangle, and Shape are same as above in abstract class approach, So i //am not mentioning it again.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;public abstract class Widget implements Drawable&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;public void draw();&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;//all widgets will extend Widget and implement draw method&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;public class Canvas&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;private List&lt;drawable&gt; items = new ArrayList&lt;drawable&gt;();&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;public void add(Drawable drawable)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;items.add(drawable);&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;public void show()&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;for (Drawable item:items)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;   &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;item.draw();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: -webkit-monospace; font-size: 13px; "&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;public class Main&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;public static void main(String args[])&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;Canvas canvas = new Canvas();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;Shape rec1 = new Rectangle();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;canvas.add(rec1);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;Widget&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; wid1 = new Widget1();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;canvas.add(wid1);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;canvas.show();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-tab-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 153, 0);"&gt;&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;So in the light of experience which we have gotten from above examples, i summarize the rule:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Use Abstract class for creating a bond between similar items like Rectangle and Triangle are similar entities so extend them from Shape&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Use interface for creating a bond between dissimilar items like Shape and Widget are different from Canvas.&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Always try to use Abstract class or interface whenever you need to upcast them to achieve simplicity e.g. add and show method of Canvas.&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;Now i discuss other properties of interface. Interfaces can also contain fields but those fields will be static and final. Interface can have public or package access. It can also be private only if defined inside of a class as inner interface.&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;div&gt;In the last i explain another problem which usually hapens when interfaces are implemented. Supppose there are two interfaces one is ShapeClonable and second one is WidgetClonable. Body of these two interfaces are given below:&lt;/div&gt;&lt;code&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public interface ShapeClonnable&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public Shape clone();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public interface WidgetClonnable&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public Widget clone();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/code&gt;&lt;br /&gt;&lt;div&gt;Now there is a class Figure which implements both interface as show below:&lt;/div&gt;&lt;code&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;public class Figure implements ShapeClonnable, WidgetClonnable&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;public Shape clone()&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;//implementation&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;public Widget clone()&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;//implementation&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color:#ff9900;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/code&gt;&lt;br /&gt;&lt;div&gt;Figure class contains two methods with same name. It is called method overloading but for that, two methods should have either different number of parameters or differnent types of parameters. In the above case, two methods differ in only return types which are not covered in method overloading, so there will be compile time error. You should give special attention in designing interfaces to avoid this type of error. &lt;/div&gt;&lt;br /&gt;&lt;div&gt; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-2294294418076233827?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/2294294418076233827/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=2294294418076233827' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/2294294418076233827'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/2294294418076233827'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2008/08/abstract-classes-and-interfaces.html' title='Abstract Classes and Interfaces'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-3782876991339393266</id><published>2008-07-23T02:51:00.000-07:00</published><updated>2008-07-24T12:53:53.972-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='class per hierarchy'/><category scheme='http://www.blogger.com/atom/ns#' term='hibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='inheritance'/><category scheme='http://www.blogger.com/atom/ns#' term='pros'/><category scheme='http://www.blogger.com/atom/ns#' term='polymorphism'/><category scheme='http://www.blogger.com/atom/ns#' term='cons'/><category scheme='http://www.blogger.com/atom/ns#' term='drawbacks'/><title type='text'>Inheritance Strategy - Class per hierarchy</title><content type='html'>Hibernate supports several types of inheritance strategies in which simplest of them is "Class-per-heirarchy". In this strategy, data for all classes involved in inheritance is stored in a single table and a record is identified by discriminator column. Lets discuss this strategy with an example. Suppose there are three classes: Person, Employee, and Physician. Inheritance relationship is shown in the fig below:&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_JaPG-95CW88/SIcSh6m30GI/AAAAAAAAACc/H1v6SvYTzrk/s1600-h/inheritance.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://4.bp.blogspot.com/_JaPG-95CW88/SIcSh6m30GI/AAAAAAAAACc/H1v6SvYTzrk/s320/inheritance.jpg" alt="" id="BLOGGER_PHOTO_ID_5226166266464030818" border="0" /&gt;&lt;/a&gt;Person is an abstract class. So either a person will exist as Employee or Physician. Hibernate mapping for Person is shown below:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_JaPG-95CW88/SIctuD-dpDI/AAAAAAAAACs/wyO3bLdueac/s1600-h/person-mapping.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://3.bp.blogspot.com/_JaPG-95CW88/SIctuD-dpDI/AAAAAAAAACs/wyO3bLdueac/s320/person-mapping.jpg" alt="" id="BLOGGER_PHOTO_ID_5226196161951278130" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;&lt;/span&gt; Discriminator element creates a column with name "Type", value of which will indicate whether a particular record is Employee or Physician. There is also a special property "abstract" in class element which tells that object of person can't be created.&lt;br /&gt;&lt;br /&gt;Hibernate mapping for Employee is shown below:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_JaPG-95CW88/SIct0qxsNpI/AAAAAAAAAC0/DvI2HIJgbL8/s1600-h/employee-mapping.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://4.bp.blogspot.com/_JaPG-95CW88/SIct0qxsNpI/AAAAAAAAAC0/DvI2HIJgbL8/s320/employee-mapping.jpg" alt="" id="BLOGGER_PHOTO_ID_5226196275445905042" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;"subclass" element indicates that there is a class per hierarchy strategy of inheritance between Employee and Person. "extends" property of subclass element creates relationship of "Employee" with "Person". "discriminator-value" tells hibernate to use "Employee" in discriminator column to identify all employees.&lt;br /&gt;&lt;br /&gt;Configuration for Physician is given below:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_JaPG-95CW88/SIct6o3NYpI/AAAAAAAAAC8/QxJnj9Q8z5g/s1600-h/physician-mapping.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://1.bp.blogspot.com/_JaPG-95CW88/SIct6o3NYpI/AAAAAAAAAC8/QxJnj9Q8z5g/s320/physician-mapping.jpg" alt="" id="BLOGGER_PHOTO_ID_5226196378011394706" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;br /&gt;Now lets add two Persons: one is Employee and other is Physician as shown below:&lt;br /&gt;&lt;code&gt;&lt;br /&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;Session session = HibernateUtil.getSessionFactory().openSession();&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;        session.beginTransaction();&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;        Emplo&lt;/span&gt;&lt;/code&gt;&lt;code&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;yee emp = new Employee();&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;        emp.setName("Amer Sohail");&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;        emp.setPay(123f);&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;        session.save(emp);&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;        Physician phy = new Physician();&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;        phy.setName("Dr. Amer Sohail");&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;        phy.setPay(345f);&lt;br /&gt; phy.setBillingAddress("Lahore");&lt;br /&gt; phy.setSpeciality("Pediatrics"); &lt;/span&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        session.save(phy);&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;        session.getTransaction().commit();&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0);"&gt;        session.close();&lt;/span&gt; &lt;/code&gt;&lt;br /&gt;&lt;br /&gt;a single table with name "person" will be created in the database with following two records:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_JaPG-95CW88/SIcmEatiW-I/AAAAAAAAACk/5iv8GKDRVl0/s1600-h/person_table.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://1.bp.blogspot.com/_JaPG-95CW88/SIcmEatiW-I/AAAAAAAAACk/5iv8GKDRVl0/s320/person_table.jpg" alt="" id="BLOGGER_PHOTO_ID_5226187749918399458" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Lets retrieve the all person from database and you will observe polymorphic feature of hibernate:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;   &lt;span style="color: rgb(255, 153, 0);"&gt;Session session = HibernateUtil.getSessionFactory().openSession();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        session.beginTransaction();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        List emp = session.createQuery("from Person emp").list();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        if (emp != null &amp;amp;&amp;amp; emp.size() &gt; 0)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            for (int index = 0; index &lt;&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                System.out.println("Name of employee is " +((Employee)emp.get(index)).getName());&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                if (emp.get(index) instanceof Person)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    System.out.println("This is person");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                if (emp.get(index) instanceof Employee)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    System.out.println("This is Employee");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                if (emp.get(index) instanceof Physician)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;                    System.out.println("This is Physician");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        session.getTransaction().commit();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;        session.close();&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;output of the code will be following:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;Name of employee is Amer Sohail&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;This is person&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;This is Employee&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;Name of employee is Dr. Amer Sohail&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;This is person&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;This is Employee&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;This is Physician&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;As shown in output, i just retrieved "persons" but it instantiated objects of type to which they belong e.g. Amer Sohail was just an employee not a physician so hibernate created it of "Employee" type. This is really a very powerful feature of hibernate.&lt;br /&gt;&lt;br /&gt;Now if i want to retrieve only employee not physician, then i will write the following query:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;List emp = session.createQuery("from Employee emp where type = Employee").list();&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I explained the strategy "class-per-heirarchy" in this blog's post. This strategy has one major drawback which you should notice while viewing records in table. It contains "nulls" against those records which are not in employee like "speciality" and "billing_address". Since DBMS has to handle null fields with a special case and it creates an impact on performance so that is the major drawback of this strategy because it will create a lot of nulls in the table. but on the other handle, no join is needed so no join cost improves the performance. Now there is very interesting situation bcz pros of this blog says it increase the performance while cons says it decreases the performance so what is the net effect. you have to wait for it for later posts in which i will compare all the strategies.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-3782876991339393266?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/3782876991339393266/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=3782876991339393266' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/3782876991339393266'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/3782876991339393266'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2008/07/inheritance-strategy-class-per.html' title='Inheritance Strategy - Class per hierarchy'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_JaPG-95CW88/SIcSh6m30GI/AAAAAAAAACc/H1v6SvYTzrk/s72-c/inheritance.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-8112549432042311398</id><published>2008-07-19T23:13:00.000-07:00</published><updated>2008-07-20T06:54:09.437-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='recommendations'/><category scheme='http://www.blogger.com/atom/ns#' term='hibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='example'/><category scheme='http://www.blogger.com/atom/ns#' term='batch'/><category scheme='http://www.blogger.com/atom/ns#' term='session'/><category scheme='http://www.blogger.com/atom/ns#' term='first level of cache'/><category scheme='http://www.blogger.com/atom/ns#' term='sessionless'/><category scheme='http://www.blogger.com/atom/ns#' term='session cache'/><category scheme='http://www.blogger.com/atom/ns#' term='insert'/><title type='text'>Batch Insertion in Hibernate</title><content type='html'>Batch Insertion is a powerful feature of hibernate particularly useful when you are importing data from other systems in batch. If you do not use batch feature of hibernate, your application's performance may decrease dramatically at the time of insertion of many records.&lt;br /&gt;There are two approaches in hibernate to handle batch insertion. Each one is explained below:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;One approach is related to Session class. Let's discuss it with example. Suppose there is an application for library which uses hibernate as ORM layer. Now you want to insert 1 million books in library. For simplicity domain model contains two classes one is book and second one is publisher. Publisher is contained in book class and i have enabled cascade insert and cascade update so if i insert book which contains publisher that is not in the system, it will also be saved with book. Code to insert books are given below:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;        Session session =HibernateUtil.getSessionFactory().openSession();&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;        session.beginTransaction();&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;    &lt;br /&gt;for (int index = 0; index &lt;&gt;          &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;            Book book = new Book();&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;book.setAuthor("amer");&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;        &lt;br /&gt;       book.setIsbn("34343");&lt;br /&gt;      &lt;/span&gt;&lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;book.setName("Hibernate " + index);&lt;br /&gt;      &lt;/span&gt;&lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;Publisher pub = new Publisher();&lt;br /&gt;      &lt;/span&gt;&lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;pub.setName("Publisher " + index);&lt;br /&gt;&lt;/span&gt;         &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;book.setPublisher(pub);&lt;/span&gt;&lt;br /&gt;     &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;book.setPublishDate(new Date());&lt;/span&gt;&lt;br /&gt;     &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;session.save(book);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;            &lt;/span&gt; &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;        }&lt;br /&gt;&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;        &lt;/span&gt; &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;        session.getTransaction().commit();&lt;br /&gt;&lt;/span&gt; &lt;span style="color: rgb(255, 153, 0); font-style: italic;"&gt;        session.close();&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-style: italic;"&gt;                &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Above code is putting a lot of memory burden because whenever an object is saved, hibernate puts that object in cache which is called as "session cache" or "first level of cache" and probably you will face an error of Stack outofflow memory error. In order to avoid this, you have to clear the session, but question is when session should be cleared either after each insertion of after some interval. If you clear session, after each insertion, it will decrease your applications' performance dramatically because before calling clear operation on session, you have to call flush operation which will synchronize persistent data store to objects memory states. For this situation, there comes a concept of batch insertion. For batch insertion, first of all you will have to add "hibernate.jdbc.batch_size" property in your hibernate.cfg.xml file with value of 50.&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 153, 0);"&gt;&lt;property name="hibernate.jdbc.batch_size"&gt;&lt;property name="hibernate.jdbc.batch_size"&gt;&lt;property name="hibernate.jdbc.batch_size"&gt;&lt;br /&gt;&lt;/property&gt;&lt;/property&gt;&lt;/property&gt;&lt;/span&gt;By using this property, hibernate will use jdbc for batch insertion of 50 records at a time when you flush the session. Batch size "50" is my recommendation because i always got max application performance with this number. You also have to change code to flush objects from session as below:&lt;br /&gt;&lt;br /&gt;    &lt;span style="font-style: italic; color: rgb(255, 153, 0);"&gt;session.save(book);&lt;/span&gt; &lt;span style="font-style: italic; color: rgb(255, 153, 0);"&gt;            if (index % 50== 0)&lt;/span&gt; &lt;span style="font-style: italic; color: rgb(255, 153, 0);"&gt;            {&lt;/span&gt; &lt;span style="font-style: italic; color: rgb(255, 153, 0);"&gt;                session.flush();&lt;/span&gt; &lt;span style="font-style: italic; color: rgb(255, 153, 0);"&gt;                session.clear();&lt;/span&gt; &lt;span style="font-style: italic; color: rgb(255, 153, 0);"&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Second way of doing batch insertion is through "StatelessSession" class. StatelessSession class differs from Session class in that it does not cache the objects, does not call interceptors, does not save any persistence context of object, does not cascade to composed objects, does not take care of collections, directly transfers the object to jdbc insert statement. So in other way it is more close to jdbc. With StatelessSession, you have to save composed objects separately e.g. in above example, you have to insert publisher and books separately. Above code with StatelessSession will be:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;StatelessSession session = HibernateUtil.getSessionFacgtory().openStatelessSession();&lt;/span&gt; &lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;        session.beginTransaction();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;for (int index = 0; index &lt; style="font-style: italic; color: rgb(255, 153, 102);"&gt;        {&lt;br /&gt;&lt;/span&gt;      &lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;            Book book = new Book();&lt;/span&gt;&lt;br /&gt;    &lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;book.setAuthor("amer");&lt;br /&gt;    &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;book.setIsbn("34343");&lt;/span&gt;&lt;br /&gt;    &lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;book.setName("Hibernate " + index);&lt;/span&gt;&lt;br /&gt;    &lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;Publisher pub = new Publisher();&lt;br /&gt;    &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;pub.setName("Publisher " + index);&lt;br /&gt;    &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;book.setPublisher(pub);&lt;/span&gt;&lt;br /&gt;    &lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;book.setPublishDate(new Date());&lt;br /&gt;    &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;session.insert(pub);&lt;/span&gt;&lt;br /&gt;    &lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;session.insert(book);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;            &lt;/span&gt; &lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;        }&lt;br /&gt;&lt;/span&gt; &lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;        &lt;/span&gt; &lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;        session.getTransaction().commit();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 153, 102);"&gt;        session.close();&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Well its time now to conclude the things. I have used both  ways and i prefer first approach over second one because of the following reasons:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;StatelessSession does not provide any performance over Session when you have to save composed instances too.&lt;/li&gt;&lt;li&gt;It does not call any interceptors or evens which may complicate the application design if we heavily depend on interceptors or events for logging or data level security etc.&lt;/li&gt;&lt;li&gt;Even for a plain object, StatelessSession does not provide me much performance over Session. It only provided me performance when an object contains one or two attributes. e.g. if i only insert publisher through StatelessSession, i will get performance by 20 to 30 seconds.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-8112549432042311398?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/8112549432042311398/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=8112549432042311398' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8112549432042311398'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8112549432042311398'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2008/07/batch-insertion-in-hibernate.html' title='Batch Insertion in Hibernate'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-1427806976812604394</id><published>2008-07-05T23:52:00.000-07:00</published><updated>2008-07-20T05:40:19.599-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='package'/><category scheme='http://www.blogger.com/atom/ns#' term='java archive'/><category scheme='http://www.blogger.com/atom/ns#' term='library'/><category scheme='http://www.blogger.com/atom/ns#' term='executable'/><category scheme='http://www.blogger.com/atom/ns#' term='manifest'/><title type='text'>Packaging j2se application in jar</title><content type='html'>Jar file is distributable unit for j2se applications. It is a zipped file but with jar extension. Jar file contains following contents:&lt;br /&gt;&lt;br /&gt;- Java compiled classes&lt;br /&gt;- manifest.mf file which contains meta info regarding jar like name of main class, jars to be added in classpath etc, version info etc. (optional)&lt;br /&gt;- other jars on which classes of this jar depends (optional)&lt;br /&gt;&lt;br /&gt;There are two types of jars:&lt;br /&gt;&lt;br /&gt;- Library Jar&lt;br /&gt;- Executable Jar&lt;br /&gt;&lt;br /&gt;Difference between the two jars is simply a main class. If your application runs standalone and contains main class, then you put name of main class in manifest file. This makes a jar an executable jar. You can run your executable jar just by double clicking on it or through java command with switch -jar. On the other hand, library jar does not contain any main class, and it always runs as a dependency jar for other java applications.&lt;br /&gt;&lt;br /&gt;Now lets see how we can create a library jar&lt;br /&gt;If you project just contains java classes and no other jars or manifest file, then you will create jar using following command:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;project-folder&gt;jar cvf mylibrary.jar -C classes .&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;cvf are three switches for jar. "c" means i want to create a new jar rather than updating previous one. "v" means i want to print all log info on console. "f" means supply an output to file i.e. mylibrary.jar in this case. -C is another switch which is used to tell which folders you want to include in jar. In my case all compiled java files are under classes folder but i don't want to include classes folder itself in jar so i used "-C classes .". Dot "." here tells jar command not to include classes folder. If you omit ".", then you have to change switch -C with this "-C classes/". it will tell jar command that all compiles files are under classes folder and include classes folder itself in jar file.&lt;br /&gt;if you look at the contents of created jar file using any unzipped software, you will find that it contains classes + one more folder named as "META-INF". This folder contains manifest.mf. By default jar creates manifest.mf itself if you don't provide it. If you open this manfiest.mf using any text editor, you will see it just contains following two entries:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;Manifest-Version: 1.0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;Created-By: 1.6.0_03 (Sun Microsystems Inc.)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Important one is "Created-By" which tells which version of java was used to package jar file. In this case it is jse 6 with update 3.&lt;br /&gt;If classes depend on other jars, you have to include those jars in classpath. This can be done at the time of running a program or writing all dependencis in manifest file, so java will automatically add it to classpath. Suppose above jar "mylibrary.jar" depends on other jars named as "other1.jar" and "other2.jar". Now to include these two jars in classpath through manifest file. Create a text file with any name let dependencies.txt and add following lines:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;Class-Path: other1.jar other2.jar&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Keep in mind that you have to provide spaced in between the every two jar file names.&lt;br /&gt;&lt;br /&gt;Now you can add this manifest file to already created manifest file using following command:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;project-folder&gt; jar uvfm mylibrary.jar dependencies.txt&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Console output will show that Manifest.mf updated. In this command i used switch "u" instead of "c" because i want to append manifest in already created jar. I also used "m" switch, which is used when you want to provide your own manifest file. If i want to create new jars with my own manifest file, then i have to use following command:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;project-folder&gt; jar cvfm mylibrary.jar dependencies.txt -C classes .&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It is the same command as used previously to create jar with exception of extra "m" switch and name of manifest file.&lt;br /&gt;&lt;br /&gt;Procedure of creating executable jar file is  the same as library jar but you have to provide one extra attribute in your manifest file with name "Main-Class", So if i have name of main class as "org.engineers.java.MyMain", then i will have to added one entry in manifest file as:&lt;br /&gt;&lt;br /&gt;Main-Class: org.engineers.java.MyMain&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-1427806976812604394?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/1427806976812604394/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=1427806976812604394' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/1427806976812604394'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/1427806976812604394'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2008/07/packaging-j2se-application-in-jar.html' title='Packaging j2se application in jar'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-7865752638319899429</id><published>2008-06-21T11:14:00.000-07:00</published><updated>2008-07-05T23:34:10.977-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='classpath'/><category scheme='http://www.blogger.com/atom/ns#' term='program'/><category scheme='http://www.blogger.com/atom/ns#' term='package'/><category scheme='http://www.blogger.com/atom/ns#' term='-cp'/><category scheme='http://www.blogger.com/atom/ns#' term='executing'/><category scheme='http://www.blogger.com/atom/ns#' term='compilation'/><category scheme='http://www.blogger.com/atom/ns#' term='running'/><category scheme='http://www.blogger.com/atom/ns#' term='compiling'/><title type='text'>Compiling and Executing Java program</title><content type='html'>&lt;div&gt;There are three basic steps in development of a simple java program which are Coding, Compiling, and Executing the java program. Coding is to write different set of classes and interfaces using any text editor like notepad etc. There are three cases which a java program can have. Those are:&lt;br /&gt;&lt;br /&gt;Case A: Classes in default package (no package) and does not use classes which are bundled in jar.&lt;br /&gt;Case B: Classes which are grouped in different packages but does not use classes which are bundled in jar.&lt;br /&gt;Case C: Classes which are group in different packages and also uses classes bundled in jar.&lt;br /&gt;&lt;br /&gt;Now i discuss compilation and execution of java program in each case one by one.&lt;br /&gt;&lt;br /&gt;Lets discuss Case A. It is most simplest case which does not have any dependency on classes of others which are bundled in jar.  Morever, classes are not grouped in different packages. Classes which are not grouped in packages, are said to be contained in default package. Suppose you have created a folder named as "TestProject" and has placed all java files there, then you can do following in windows to compile and exeuctge java program:&lt;br /&gt;Just go to directory "TestProject" through command prompt and write following command:&lt;br /&gt;C:\TestProject&gt;javac *&lt;br /&gt;above command will compile all java files. Now In order to run the java program, write following command:&lt;br /&gt;C:\TestProject&gt;java MainClass&lt;br /&gt;where MainClass is any class which contains "public static void main(String[] args)" method.&lt;br /&gt;&lt;br /&gt;Now we discuss Case B in which classes are grouped under packages. Packages provide better management of code, easy maintenance, and good access control on classes. Morever, packages provide safety from class name collissions. This case has further two possibilities. One is that you have logical grouped java classes in packages but placed them together like in case A i.e. all classes in folder TestProject. Now you will use following command to compile the code:&lt;br /&gt;C:\TestProject&gt;javac * -d .&lt;br /&gt;"d" switch is used to provide destination for compiled classes. When destination is ".", it means root directory (TestProject in this case) will be default directory. javac command will first create folder heirarchy similar to packages and will place compiled class files in respective folder e.g. if a class is in package "org.engineers.project", then javac will first create folder named as "org" at root directory, then will create "engineers" folder under "org" and "project" folder under "engineers" folder. and will place class in "project" folder. In order to execute java program, you have to write following command at root directory:&lt;br /&gt;C:\TestProject&gt;java fully_qualified_class_name&lt;br /&gt;fully_qualified_class_name means name of class with package e.g. if a class "Main" is contained under package org.engineers.project, then fully_qualified_class_name will be "org.engineers.project.Main"&lt;br /&gt;&lt;br /&gt;Now we talk about second possibility in Case B which is that you have placed java source files in same folder hierarchy, as of packages i.e. if a class "Main.java" is in package "org.engineers.project", you have placed Main.java in folder org/engineers/project folder. You can use same command to compile and execute java program as in first possibility but it will mix your compiled code with source code so in order to keep them separate create a directory named as "src" under root directory (TestProject in this case) and then place all folder hierarchy containing source files there. Now create another folder named as "classes" at root level and use following command to compile the code:&lt;br /&gt;C:\TestProject&gt;javac * -d classes&lt;br /&gt;"classes" folder be treated as root folder for class files.&lt;br /&gt;In order to run java program, you have to change java command slightly. New command will be:&lt;br /&gt;C:\TestProject&gt;java -cp classes org.engineers.project.Main&lt;br /&gt;Difference with this command and with previous command is of addition switch named as "cp". it represents classpath and tells java command  where to find compiled classes. Since compiled classes are in "classes" folder, so i added that folder in classpath.&lt;br /&gt;&lt;br /&gt;Now its time to discuss Case C, which is more realistic case. In this case, java source files uses classes which are placed in "jar" file. Jar file is just a zip file but with extensino java archive ("jar") . Now you will have to include all jar files in classpath using same switch which we used in preceding case to run the program. but this time we will have to use switch in both javac and java command.&lt;br /&gt;C:\TestProject&gt; javac -cp test.jar * -d classes&lt;br /&gt;where test.jar is jar file which contains other classes which my classes use.&lt;br /&gt;To execute the java program use following command:&lt;br /&gt;C:\TestProject&gt; java -cp test.jar;classes org.enigneers.project.Main&lt;br /&gt;Note that i have added both "test.jar" and "classes folder in classpath.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-7865752638319899429?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/7865752638319899429/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=7865752638319899429' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/7865752638319899429'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/7865752638319899429'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2008/06/classpat.html' title='Compiling and Executing Java program'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-237672869152753599</id><published>2008-06-14T12:44:00.000-07:00</published><updated>2008-06-15T04:14:44.078-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='best practices'/><category scheme='http://www.blogger.com/atom/ns#' term='implementing Cloneable'/><category scheme='http://www.blogger.com/atom/ns#' term='Clone'/><category scheme='http://www.blogger.com/atom/ns#' term='overriding clone method'/><category scheme='http://www.blogger.com/atom/ns#' term='Cloneable interface'/><category scheme='http://www.blogger.com/atom/ns#' term='Things to consider while cloning of objects'/><category scheme='http://www.blogger.com/atom/ns#' term='deep cloning'/><category scheme='http://www.blogger.com/atom/ns#' term='shallow cloning'/><category scheme='http://www.blogger.com/atom/ns#' term='CloneNotSupportedException'/><title type='text'>Cloning of Java objects</title><content type='html'>Parent class of all objects in java is "Object" which contains several important methos. One of them is "clone" which is used to make a copy of object. There are certain things which you should keep in mind while using clone method of Object.&lt;br /&gt;- implement "Cloneable" interface to each class which you want to clone. If a class does not implement "Cloneable" interface, clone method will throw "CloneableNotSupportedException". This interface does not contain any methods. It is just a marker interface. Suppose you have a Class "Book" of which you want to make a clone, then structure of that class will be:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;public class Book implements Cloneable&lt;br /&gt;{&lt;br /&gt;//attributes&lt;br /&gt;//methods&lt;br /&gt;@Override protected Object clone() throws CloneableNotSupportedException&lt;br /&gt;{&lt;br /&gt;//Clone logic&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;- Obtain cloned object through super.clone&lt;br /&gt;- if your class only contains primitive attributes or wrappers of primitive attributes or immutable object like String, then you don't need to do anything special inside clone method because clone method automatically makes a copy of primitive data types and immutable objects. Suppose above "Book" class contains following two attributes with accessor methdos for attributes:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;private String name;&lt;br /&gt;private Float price;&lt;br /&gt;public String getName() {&lt;br /&gt;return name;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setName(String name) {&lt;br /&gt;this.name = name;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public Float getPrice() {&lt;br /&gt;return price;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setPrice(Float price) {&lt;br /&gt;this.price = price;&lt;br /&gt;}&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;Now to clone Book class you just need to write following implementation in overriding of clone method:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;protected Object clone() CloneNotSupportedException&lt;br /&gt;{&lt;br /&gt;return super.clone();&lt;br /&gt;}&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;- by default clone method makes shallow copy and does not make a Deep copy of object. Deep copy means if your object contains references of other objects or collections, it will not make a copy of those referenced objects and will use same references as was in origianl objects. Suppose above mentioned "Book" class contains an attribute of class "Publisher". Structure of Publisher class is given below:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;em&gt;public class Publisher{&lt;br /&gt;private String pubName;&lt;br /&gt;&lt;br /&gt;public String getPubName() {&lt;br /&gt;return pubName;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setPubName(String pubName) {&lt;br /&gt;this.pubName = pubName;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;Now try out the following code:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Publisher pub = new Publisher();&lt;br /&gt;pub.setPubName("Java Publisher");&lt;br /&gt;orig.setPub(pub);&lt;br /&gt;Logger.getLogger(CloneTest.class.getName()).log(Level.INFO,orig.getPub().getPubName());&lt;br /&gt;Book clone2 = (Book) orig.clone();&lt;br /&gt;Logger.getLogger(CloneTest.class.getName()).log(Level.INFO,clone2.getPub().getPubName());&lt;br /&gt;orig.getPub().setPubName("java Publisher Changed");&lt;br /&gt;Logger.getLogger(CloneTest.class.getName()).log(Level.INFO,clone2.getPub().getPubName());&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;Output of above code will be:&lt;br /&gt;&lt;span style="color:#ff6600;"&gt;15-Jun-2008 1:39:47 AM javasolutionsblog.clonetest.CloneTest main&lt;br /&gt;INFO: Java Publisher&lt;br /&gt;15-Jun-2008 1:39:47 AM javasolutionsblog.clonetest.CloneTest main&lt;br /&gt;INFO: Java Publisher&lt;br /&gt;15-Jun-2008 1:39:47 AM javasolutionsblog.clonetest.CloneTest main&lt;br /&gt;INFO: Java Publisher Changed &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Although expected output of third log was "Java Publisher" but since clone didn't make copy of refernce object publisher, it used same object in clone as in origignal object, when i make a change in the publisher name in original object, it also reflected in cloned object.&lt;br /&gt;&lt;br /&gt;- In order to do deep copy of Book class, we will aso have to implement "Cloneable" interface in "Publisher" class + override clone method in Publisher class too. If Publisher class contains references of other objects, then you will have to implement Cloneable interface + override clone method in each sub class too.&lt;br /&gt;In our case, structure of Publisher class will be:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;public class Publisher implements Cloneable{&lt;br /&gt;private String pubName;&lt;br /&gt;&lt;br /&gt;public String getPubName() {&lt;br /&gt;return pubName;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setPubName(String pubName) {&lt;br /&gt;this.pubName = pubName;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected Object clone() throws CloneNotSupportedException&lt;br /&gt;{&lt;br /&gt;return super.clone();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;Now change the definition of clone method of Book class as follows:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;protected Object clone() throws CloneNotSupportedException {&lt;br /&gt;Book clonedBook = (Book) super.clone();&lt;br /&gt;if (this.getPub() != null) {&lt;br /&gt;clonedBook.setPub((Publisher) this.getPub().clone());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return clonedBook;&lt;br /&gt;}&lt;/em&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-237672869152753599?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/237672869152753599/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=237672869152753599' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/237672869152753599'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/237672869152753599'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2008/06/cloning-of-java-objects.html' title='Cloning of Java objects'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-8894507978153008022</id><published>2008-06-12T11:15:00.000-07:00</published><updated>2008-06-14T10:31:01.995-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='best practices'/><category scheme='http://www.blogger.com/atom/ns#' term='overriding equals and hashCode'/><category scheme='http://www.blogger.com/atom/ns#' term='equality check'/><category scheme='http://www.blogger.com/atom/ns#' term='object comparison'/><category scheme='http://www.blogger.com/atom/ns#' term='hashcode'/><category scheme='http://www.blogger.com/atom/ns#' term='equals'/><title type='text'>Implementing Equals and HashCode</title><content type='html'>In my last blog i described how we can compare two java objects of same class by overriding equals method. In this blog i will explain best practices regarding implementing of equals method.&lt;br /&gt;Whenever, we go for overriding of equals method, one question comes to our mind is, on which basis we will have to say that two objects are same. First answer comes to mind is that we should compare objects on the basis of primary key which is also used to distinguish the records in the database. Primary keys are good candidate to compare objects if we assign primary keys ourself not by database. When primary key is assigned by database like auto number, objects don't have primary key until they are persisted in the database so before persistent of objects,  how we should compare the objects. Good practice is always compare objects on the basis of business key which uniquely describes objects. Suppose we have a class of Person with attributes (id, name, ssn,address) .  In this class we have two unique attributes one is id and second one is ssn. id is used to distinguish the objects in the database and is assigned when objects are persisted, then only choice is SSN which we can use to compare different person because each person has his own SSN (Social Security Number). So best practice is always compare objects on business keys rather than on database keys.&lt;br /&gt;Second best practice is whenever you go for overriding of equals method. also override "int hashCode()" method. This method returns hash of objects. Implementing hasCode facilitates us when we use objects as keys in HashTable. If we don't override hashCode method, each object created with new operator will always has different hash values irrespective of fact whether they are same or not. Suppose I have a class of Customer with attributes (id, name, ssn, address) and i create two objects with new operation having the same dataset. Although logically they are same but physically they are two different objects that's y they have differnt hashCode. So best practice is that if two objects are equals, then those two objects should always have same hashCode. Overriding of hashCode will also facilitate us when we save objects in sets. Sets store only one instance of an object at a time, so if we have two customer objects with same attributes, then only one instance of customer must be saved in sets. It is only possible by overriding of hashcode method.&lt;br /&gt;Now question is how to generate hashCode for an object. Answer is same as for equals method. Always generate hashCode from business keys. In above example of Customer objects, we should generate hashCode of Customer from SSN. java has provided methods which generate hashCode for all primitive data types and for String. hashCode for primitive data types are generated by using wrappers of primitive data types e.g. for int we can generate hashCode by following code:&lt;br /&gt;&lt;br /&gt;Integer i = new Integer(5);&lt;br /&gt;int code = i.hashCode();&lt;br /&gt;&lt;br /&gt;Similary we can get hashCode of other primitive datatypes by using respective wrapper classes.&lt;br /&gt;&lt;br /&gt;So in this blog we have come to Two Best Practices regarding comparison of objects:&lt;br /&gt;- One is always compare objects in equal method on the basis of business keys.&lt;br /&gt;- Second is whenever override equals method, also override hashCode method and generate hash for objects on the basis of same business key as used in equals method.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-8894507978153008022?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/8894507978153008022/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=8894507978153008022' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8894507978153008022'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8894507978153008022'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2008/06/implementing-equals-and-hashcode.html' title='Implementing Equals and HashCode'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-1353567098963952947</id><published>2008-05-03T04:15:00.000-07:00</published><updated>2008-05-03T04:59:01.648-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='== operator'/><category scheme='http://www.blogger.com/atom/ns#' term='equality check'/><category scheme='http://www.blogger.com/atom/ns#' term='object comparison'/><category scheme='http://www.blogger.com/atom/ns#' term='equals'/><title type='text'>Comparison of Objects with equals</title><content type='html'>== relational operator is used to compare basic data types in java e.g. if we want to compare two integers, we will do it as follows:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;int a = 5;&lt;/em&gt;&lt;br /&gt;&lt;em&gt;int b = 5;&lt;/em&gt;&lt;br /&gt;&lt;em&gt;if (a == b)&lt;/em&gt;&lt;br /&gt;&lt;em&gt;System.out.println("equal");&lt;/em&gt;&lt;br /&gt;&lt;em&gt;else&lt;/em&gt;&lt;br /&gt;&lt;em&gt;System.out.println("not equal");&lt;/em&gt;&lt;br /&gt;&lt;em&gt;&lt;/em&gt;&lt;br /&gt;Output of above java code will be "equal". but this "==" operator works only with basic data types, even it will not work if we use wrapper classes of basic data types e.g.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Integer a = new Integer(5);&lt;/em&gt;&lt;br /&gt;&lt;em&gt;Integer b = new Integer(5);&lt;/em&gt;&lt;br /&gt;&lt;em&gt;if (a == b)&lt;br /&gt;System.out.println("equal");&lt;br /&gt;else&lt;br /&gt;System.out.println("not equal");&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;Output of above code will be "not equal". Why "==" operator does not applicable for objects is that because it compares two references of object not contents of objects. Now for comparison of contents of objects there is "equals" method in each object e.g. if we change above comparison of wrapper classes with this code, it will work correctly:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Integer a = new Integer(5);&lt;br /&gt;Integer b = new Integer(5);&lt;br /&gt;if (a.equals(b)) &lt;/em&gt;&lt;br /&gt;&lt;em&gt;System.out.println("equal");&lt;/em&gt;&lt;br /&gt;&lt;em&gt;else &lt;/em&gt;&lt;br /&gt;&lt;em&gt;System.out.println("not equal");&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;Output of above code will be "equal" This "equals" work fine for wrapper classes but does not run correctly for custom classes because it compares two object references by default just like "==" operator. To change behaviour of "equals" we will have to override it in our classes. Suppose i have a class "Book" which has attributes like name, isbn, author, publisher. Now uniqueness of book is checked using isbn number so to compare two books, override "equals" method and in that implementation check the "isbn" of book. Code Structure is given below:&lt;br /&gt;&lt;br /&gt;public class Book{&lt;br /&gt;private String isbn;&lt;br /&gt;private String name;&lt;br /&gt;private String author;&lt;br /&gt;&lt;br /&gt;public String getIsbn() {&lt;br /&gt;return isbn;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setIsbn(String isbn) {&lt;br /&gt;this.isbn = isbn;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String getName() {&lt;br /&gt;return name;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setName(String name) {&lt;br /&gt;this.name = name;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String getAuthor() {&lt;br /&gt;return author;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setAuthor(String author) {&lt;br /&gt;this.author = author;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean equals(Object obj) {&lt;br /&gt;Book book = (Book)obj;&lt;br /&gt;if (this.isbn.equals(book.getIsbn()))&lt;br /&gt;return true;&lt;br /&gt;&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void main(String args[]) {&lt;br /&gt;Book b1 = new Book();&lt;br /&gt;b1.setName("Java");&lt;br /&gt;b1.setAuthor("Amer");&lt;br /&gt;b1.setIsbn("12334");&lt;br /&gt;Book b2 = new Book();&lt;br /&gt;b2.setName("Java");&lt;br /&gt;b2.setAuthor("Amer");&lt;br /&gt;b2.setIsbn("12334");&lt;br /&gt;if (b1.equals(b2))&lt;br /&gt;System.out.println("equal");&lt;br /&gt;else&lt;br /&gt;System.out.println(" not equal");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;output of above code will be equal. even if name or author of two are different because my implementation of "equals" just checks for "isbn".&lt;br /&gt;&lt;br /&gt;In my later post, i will exlain best practices related to overriding of "equals" method.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-1353567098963952947?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/1353567098963952947/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=1353567098963952947' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/1353567098963952947'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/1353567098963952947'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2008/05/comparison-of-objects-with-equals.html' title='Comparison of Objects with equals'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6529523707014588436.post-8585828819153380848</id><published>2008-04-25T04:53:00.000-07:00</published><updated>2008-04-25T05:16:39.406-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='benefits'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='advantages'/><title type='text'>Object Oriented Programming vs Non Object Programming</title><content type='html'>&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;Often people are confused about real benefits which are provided by OOP. I am trying to remove their confusion in this episode of blog. Every innovation in this world is related to solution of a problem. There is a problem, then we try to find out solution of that problem. So we have to work in two spaces. one is Problem-space in which we concentrate on problem only. and second one is Solution-space in which we develop solution. When we develop solution through computer, we have to think in terms of machine which happened in assembly language. It is impossible for one to get a sense of problem while reading an assembly program. So there is a big gap between problem and solution. People have to develop different artifacts to bridge this gap. After assembly, some other languages came like C, prolog and others , which provided some abstraction on machine but still there was a big gap e.g. prolog concentrates on reasoning and facts. After that OOP made its position, which really reduced the gap between problem-space and solution-space. What we understand and see in problem, we can easily map to a solution which provides us good sense of problem. I explain it with an example e.g. we are developing a solution for a library. Now everyone knows that library has some books and students draw books from library for some time and then return to library. Now if we think on problem, we will come to know that everything in library is relating to three entities Book, Students, and Library itself. Now when we develop solution of this in OOP, we will map these entities to classes and by taking a general look on it, one can easily understand what's going on in the solution and to what things this solution is related.&lt;br /&gt;In short it is the OOP which reduced the gap between problem-space and soluton-space.&lt;blockquote&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6529523707014588436-8585828819153380848?l=javainnovations.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javainnovations.blogspot.com/feeds/8585828819153380848/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6529523707014588436&amp;postID=8585828819153380848' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8585828819153380848'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6529523707014588436/posts/default/8585828819153380848'/><link rel='alternate' type='text/html' href='http://javainnovations.blogspot.com/2008/04/object-oriented-programming-vs-non.html' title='Object Oriented Programming vs Non Object Programming'/><author><name>amer sohail</name><uri>http://www.blogger.com/profile/03123932514283187533</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp2.blogger.com/_JaPG-95CW88/SBHMlS-y-bI/AAAAAAAAAB8/uaCc1spGL1M/S220/amer.JPG'/></author><thr:total>0</thr:total></entry></feed>
