<?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-956601610404846539</id><updated>2011-04-21T16:20:31.880-07:00</updated><title type='text'>Untouched Java Topics</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://javauntouched.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/956601610404846539/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://javauntouched.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>IndianMonk</name><uri>http://www.blogger.com/profile/13700107805684896955</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://bp2.blogger.com/_i0yqmFapKKQ/SB14uYviCmI/AAAAAAAABMA/3T8U976AXzY/S220/closeup.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-956601610404846539.post-7871706906741381829</id><published>2006-11-26T05:21:00.000-08:00</published><updated>2006-11-26T23:10:50.848-08:00</updated><title type='text'>Insight into Inner classes</title><content type='html'>&lt;span style="FONT-WEIGHT: bold;font-family:georgia;" &gt;Is there any advantages to Java's Inner Classes?&lt;br /&gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;Believe me there are advantages of using java's inner classes.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;But before going into details about advantages let's try to answer one question "what is Inner class?"&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;Inner class:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;Declaration of a class within another class is called an "Inner Class".&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;It is basically a name of nested classes.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;In other words..&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;A class that is declared within another class or interface, is called a nested class[inner class].&lt;/span&gt;&lt;span style="FONT-WEIGHT: bold;font-family:georgia;" &gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;A normal class is a direct member of a package, a top-level class.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;Inner classes, which became available with Java 1.1, come in four flavors:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;ol style="FONT-FAMILY: georgia"&gt;&lt;li&gt;Static Member classes&lt;/li&gt;&lt;li&gt;Member classes&lt;/li&gt;&lt;li&gt;Local classes&lt;/li&gt;&lt;li&gt;Anonymous classes&lt;br /&gt;Let's take quick look at each in turn..&lt;/li&gt;&lt;/ol&gt;&lt;span style="FONT-WEIGHT: bold;font-family:georgia;" &gt;Static Member classes/Interfaces: &lt;/span&gt;&lt;span style="font-family:georgia;"&gt;Like any other static method, a static member class has access to all static methods of the parent, or top-level class.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;Since it is defined as "static" member of enclosing class it can be accessed or instantiated without any instance of an enclosing class.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote style="FONT-FAMILY: georgia"&gt;Sample code:&lt;br /&gt;class Outer {&lt;br /&gt;&lt;br /&gt;static class Inner {&lt;br /&gt;// Nested Top-level class or static nested class&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;How to access Inner class?&lt;br /&gt;Outer.Inner theInner = new Outer.Inner();&lt;br /&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;span style="font-family:georgia;"&gt;Note: There is no such thing as an member inner interface. Because, Interface are always implicitly static. They are always top-level, not member inner.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-WEIGHT: bold"&gt;Member classes: &lt;/span&gt;It is defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;blockquote&gt;Sample Code:&lt;br /&gt;&lt;br /&gt;class Outer{&lt;br /&gt;class Inner{&lt;br /&gt;// Member or Non-static inner class&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;How to instantiate member class?&lt;br /&gt;Outer.Inner theInner = new Outer().new Inner();&lt;br /&gt;or&lt;br /&gt;Outer theOuter = new Outer();&lt;br /&gt;Outer.Inner theInner = theOuter.new Inner();&lt;br /&gt;&lt;br /&gt;Note: Member classes cannot have any static member unless it is a complie-time constant[final static field].&lt;br /&gt;&lt;br /&gt;class Outer3{&lt;br /&gt;class Inner3{&lt;br /&gt;&lt;span style="FONT-WEIGHT: bold"&gt;static final int x = 0;// allowed&lt;br /&gt;// static int b = 0; // invalid&lt;br /&gt;int j =0;// okay.&lt;br /&gt;&lt;/span&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/blockquote&gt;&lt;span style="FONT-WEIGHT: bold"&gt;Local Classes:&lt;span style="FONT-WEIGHT: bold"&gt; &lt;/span&gt;&lt;/span&gt;Local classes are declared within a block of code and are visible only within that block,just as any other method variable.&lt;span style="FONT-WEIGHT: bold"&gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;/span&gt;&lt;/span&gt;&lt;blockquote&gt;&lt;span style="FONT-WEIGHT: bold"&gt;&lt;span style="FONT-WEIGHT: bold"&gt;Sample code&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;class Outer{&lt;br /&gt;void method(){&lt;br /&gt;class Inner {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;/blockquote&gt;&lt;span style="FONT-WEIGHT: bold"&gt;Anonymous Classes:&lt;/span&gt; Annoymous class are nothing but local classes which does not have name.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;Sample Code:&lt;br /&gt;&lt;br /&gt;void initUserInterface() {&lt;br /&gt;//declaration&lt;br /&gt;//...&lt;br /&gt;addWindowListener(//&lt;br /&gt;//class declaration&lt;br /&gt;new WindowAdapter(){&lt;br /&gt;//implementation for the method&lt;br /&gt;public windowClosing(WindowEvent e){&lt;br /&gt;System.exit(0);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Visit here to get more detail on Inner class&lt;br /&gt;&lt;a href="http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html"&gt;http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html&lt;/a&gt;&lt;br /&gt;&lt;/blockquote&gt;After giving brife discription about Inner classes,&lt;br /&gt;Now its time to jump insight Inner classes&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;When you are designing a class hierarchy, you try to figure out the relationship between the classes that you decide upon. These relationships more often than not can be classified as "is a" relationship and "has a" relationship.&lt;br /&gt;The "is a" relationship leads to the inheritance hierarchies, as the clause suggests, the derived class "is a" kind of super class. &lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;a href="http://www.developer.com/java/article.php/899361"&gt;click here &lt;/a&gt;for example : url &lt;a href="http://www.developer.com/java/article.php/899361"&gt;http://www.developer.com/java/article.php/899361&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;The other relationship in object-oriented design is the "has a" relationship. This containership design is perhaps a more-used design strategy than the inheritance one.&lt;br /&gt;&lt;a href="http://www.developer.com/java/article.php/899361"&gt;click here&lt;/a&gt; for example : url &lt;a href="http://www.developer.com/java/article.php/899361"&gt;http://www.developer.com/java/article.php/899361&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;There is, however, a third, less obvious, still fairly used relationship between classes, I call it the "facet" relationship. For this type of relationship Inner classes comes into picture.&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;Suppose there is a requirement (by an automobile design program) to have access to all the different types of bolts used in the automobile. Now, there could be various types of bolts used, with different dimensions, size, number of threads, etc., and we need to iterate over them to either find ones that suit our requirement, of say size, or perform some calculation of, say, finding the median weight of all the bolts. &lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="FONT-FAMILY: georgia"&gt;What we want is an "iterator" over the bolts. Inner classes are most suited for this kind of requirement. We define an Iterator class inside the class Automobile and return a reference to it through a public method &lt;code&gt;bolIterator()&lt;/code&gt;. Better still, we may make the inner class anonymous, the idea is that the "name" of this inner class is going to be used only once (when its object is created in the &lt;code&gt;iterator()&lt;/code&gt; method of the enclosing class), so we might do away with the name. &lt;/p&gt;&lt;p style="FONT-FAMILY: georgia"&gt;&lt;pre&gt;&lt;blockquote&gt;import java.util.Iterator;&lt;br /&gt;class Automobile {&lt;br /&gt; Bolt[ ] boltArray;&lt;br /&gt;  ......&lt;br /&gt; ......&lt;br /&gt; public Iterator boltIterator() {&lt;br /&gt;     return new Iterator () {&lt;br /&gt;       int count;&lt;br /&gt;       public boolean hasNext() {&lt;br /&gt;         return (count  &lt;&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p style="FONT-FAMILY: georgia"&gt;Here, the methods &lt;code&gt;next()&lt;/code&gt; and &lt;code&gt;hasNext()&lt;/code&gt; of the interface Iterator may access the private members of the class Automobile. Note here that the class Automobile has nothing to do with this behavior of Iterator. All it does is provide a method that hands over an Object of type Iterator through which the rest of the world can iterate over the bolts in Automobile. The Iterator here is not a behavior of the class Automobile but a "facet" of it. &lt;/p&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:georgia;"&gt;Resources:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:georgia;"&gt;For Inner class usage example&lt;/span&gt;&lt;/p&gt;&lt;span style="font-family:georgia;"&gt;&lt;blockquote&gt;&lt;p&gt;&lt;span style="font-family:georgia;"&gt;&lt;a href="http://www.developer.com/java/ent/article.php/899361"&gt;http://www.developer.com/java/ent/article.php/899361&lt;/a&gt;&lt;a href="http://www.developer.com/java/ent/article.php/903361"&gt;http://www.developer.com/java/ent/article.php/903361&lt;/a&gt;&lt;a href="http://www.developer.com/java/ent/article.php/899361"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:georgia;"&gt;&lt;/a&gt;&lt;a href="http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html"&gt;http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html&lt;/a&gt;&lt;a href="http://www.developer.com/java/ent/article.php/899361"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:georgia;"&gt;&lt;/a&gt;&lt;a href="http://www.javaworld.com/javaworld/javatips/jw-javatip75.html?page=1"&gt;http://www.javaworld.com/javaworld/javatips/jw-javatip75.html?page=1&lt;/a&gt;&lt;a href="http://www.developer.com/java/ent/article.php/899361"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:georgia;"&gt;&lt;/a&gt;&lt;a href="http://www.onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/index.html"&gt;http://www.onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/index.html&lt;/a&gt;&lt;a href="http://www.developer.com/java/ent/article.php/899361"&gt;&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;/a&gt;&lt;/span&gt;&lt;p&gt;&lt;span style="font-family:georgia;"&gt;&lt;/p&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/956601610404846539-7871706906741381829?l=javauntouched.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javauntouched.blogspot.com/feeds/7871706906741381829/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=956601610404846539&amp;postID=7871706906741381829' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/956601610404846539/posts/default/7871706906741381829'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/956601610404846539/posts/default/7871706906741381829'/><link rel='alternate' type='text/html' href='http://javauntouched.blogspot.com/2006/11/insight-into-inner-classes.html' title='Insight into Inner classes'/><author><name>IndianMonk</name><uri>http://www.blogger.com/profile/13700107805684896955</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://bp2.blogger.com/_i0yqmFapKKQ/SB14uYviCmI/AAAAAAAABMA/3T8U976AXzY/S220/closeup.JPG'/></author><thr:total>0</thr:total></entry></feed>
