<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>My Reflections on Java</title>
	<atom:link href="http://ankitgarg.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ankitgarg.wordpress.com</link>
	<description>From Core Java to Advanced Class Reversing Games!</description>
	<lastBuildDate>Tue, 09 Sep 2008 04:35:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='ankitgarg.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>My Reflections on Java</title>
		<link>http://ankitgarg.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://ankitgarg.wordpress.com/osd.xml" title="My Reflections on Java" />
	<atom:link rel='hub' href='http://ankitgarg.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Static Member Classes</title>
		<link>http://ankitgarg.wordpress.com/2008/08/21/static-member-classes-2/</link>
		<comments>http://ankitgarg.wordpress.com/2008/08/21/static-member-classes-2/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 12:45:49 +0000</pubDate>
		<dc:creator>ankitgarg</dc:creator>
				<category><![CDATA[Java Inner Classes]]></category>
		<category><![CDATA[Changes made to Inner Classes by the Compiler]]></category>
		<category><![CDATA[Inner Classes]]></category>
		<category><![CDATA[Inner Classes in Java]]></category>
		<category><![CDATA[Java Inner Classes after Compilation]]></category>
		<category><![CDATA[Static Inner Classes]]></category>
		<category><![CDATA[Static Inner Classes in Java]]></category>
		<category><![CDATA[Static Member Classes]]></category>

		<guid isPermaLink="false">http://ankitgarg.wordpress.com/?p=34</guid>
		<description><![CDATA[You can view all my Posts at jgenius.blogspot.com Suggested Path: Static Member Classes Non-Static Member Classes Local Classes Static Member Classes In this post I will tell you everything about Static Member Classes from inside out. You must have been frustrated by remembering about the rules governing Inner Classes. In this post I will tell [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ankitgarg.wordpress.com&amp;blog=4563241&amp;post=34&amp;subd=ankitgarg&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You can view all my Posts at <strong><a href="http://jgenuus.blogspot.com">jgenius.blogspot.com</a></strong></p>
<p><b>Suggested Path:</b><br />
<a href="http://jgenius.blogspot.com/2008/09/static-inner-classes.html">Static Member Classes</a><br />
<a href="http://jgenius.blogspot.com/2008/09/non-static-inner-classes.html">Non-Static Member Classes</a><br />
<a href="http://jgenius.blogspot.com/2008/09/local-classes-in-java.html">Local Classes</a></p>
<h1>Static Member Classes</h1>
<p>In this post I will tell you everything about Static Member Classes from inside out. You must have been frustrated by remembering about the rules governing Inner Classes. In this post I will tell you how Inner Classes actually work. This will help you get a deep knowledge of Inner Classes which will help you a lot. This code will tell how the compiler changes the Inner Classes. But remember, that is a some difference in the code given here and what is generated by the compiler. This article uses a simplified approach by removing and modifying some of the code generated by the compiler.<br />
Static Member Classes can also be called as Static Inner Classes. To begin with let me tell you in brief about Static Member Classes.</p>
<ul>
<li>They can be declared inside <em>top-level classes</em> or other <em>Static Member Classes</em></li>
<li>These are declared within other classes with the <code>static</code> modifier.</li>
<li>They can declare <code>static</code> and <code>non-static</code> members.</li>
<li>All <em>accessibility modifiers</em> can be used with these classes.</li>
<li>To create an object of <em>Static Member Class</em>, you don&#8217;t need an instance of its enclosing <code>class</code>.</li>
<li>An instance of enclosing <code>class</code> is not stored in an instance of a <em>Static Member Class</em>.</li>
<li>They can access <code>static</code> members of the enclosing <code>class</code> including <code>private static</code> members.</li>
<li>They can access <code>non-static</code> members of the enclosing <code>class</code>(including <code>private non-static</code> members) on an object of the enclosing <code>class</code>.</li>
<li>The reverse is also correct i.e. members of the enclosing <code>class</code> can access <code>static</code> members of the <em>Member Class</em> directly and <code>non-static</code> members of the <em>Member Class</em> on an Object of the <em>Member Class</em>.</li>
</ul>
<p>If you are astonished by the above list of information, don&#8217;t worry. In the course of this tutorial, you will come across all these facts.<br />
Now let&#8217;s begin one by one how the compiler changes different types of codes in an <em>inner class</em>. By the end you will know completely how all the code in an <em>inner class</em> is transformed to make the <em>inner class</em> a normal top level <code>class</code>.<br />
<br />First lets see how an <em>inner class</em> accesses <code>static</code> members of the enclosing <code>class</code> by their names.</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    static int a = 10;
    static class Inner
    {
        void display()
        {
            System.out.println(a);    //displays 10
        }
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>Now when you compile the program the compiler converts the <code>class Inner</code> to a normal top level <code>class</code>. But now the question arises that what will happen to <code>Statement # 8</code>. Well the answer is simple, the compiler adds the name of the enclosing <code>class</code> i.e. <code>Outer</code> before the name of the variable <code>a</code>. So after compilation, the program given above will look like this-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code after Comilation</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    static int a = 10;
}
class Outer$Inner
{
    void display()
    {
        System.out.println(Outer.a);    //displays  10
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>Note the name of the member(inner) <code>class</code> after compilation. The compiler adds the name of the enclosing <code>class</code> before the name of the member <code>class</code> seperated by a <code>$</code>. The <code>static</code> keyword is also gone as top-level classes cannot be <code>static</code>. To access the member <code>class</code> from any code outside the enclosing <code>class</code>, you have to use the syntax <code>&lt;enclosingClassName&gt;.&lt;memberClassName&gt;</code>. So to access the <code>class Inner</code> from outside the <code>class Outer</code> you will have to use the name of the <code>class</code> as <code>Outer.Inner</code>.<br />
Now you know that members of a <code>class</code> can access <code>private</code> members of the <code>class</code>. So a member <code>class</code> can also access the <code>private</code> members of the enclosing <code>class</code>. But how can the member class access <code>private</code> members of the enclosing <code>class</code> after compilation. Since after compilation the member <code>class</code> is converted into a normal top-level <code>class</code>, so it has no direct access to the <code>private</code> members of the enclosing <code>class</code>. But the compiler creates some package visible methods to make this possible.</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    private static int a = 10;
    static class Inner
    {
        void display()
        {
            System.out.println(a);    //displays 10
        }
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>Now after compilation the <code>class Inner</code> will become a top-level <code>class</code>. At <code>Statement # 8</code> the compiler adds a method call since this statement tries to access a private member of the enclosing <code>class</code>. Lets see how this happens-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code after Compilation</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    private static int a = 10;
    static int access$000()
    {
        return a;
    }
}
class Outer$Inner
{
    void display()
    {
        System.out.println(Outer.access$000()); //displays 10
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>Look how cleverly the compiler generates a method at <code>Statement # 4</code> which has access to private members of the <code>class</code>. This method returns the value of the <code>static</code> field <code>a</code>. Notice the <em>ugly</em> name of the compiler generated method. You cannot explicitly call this method in your code because the compiler will flag it as an error. If a local declaration or a field in the <em>inner class</em> or its <code>super class</code> hides the field of the enclosing <code>class</code>, you can access the field by placing the name of the enclosing <code>class</code> before the field name. It goes like this-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    static int a = 10;
    private static int b = 20;
    static class Inner
    {
        String a;
        String b;
        void display()
        {
            System.out.println(Outer.a);    //displays 10
            System.out.println(Outer.b);    //displays 20
        }
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>The compiler generated code will look like this-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code after Compilation</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
16.<br />
17.<br />
18.<br />
19.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    static int a = 10;
    private static int b = 20;
    static int access$000()
    {
        return b;
    }
}
class Outer$Inner
{
    String a;
    String b;
    void display()
    {
        System.out.println(Outer.a);            //displays 10
        System.out.println(Outer.access$000()); //displays 20
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>Now that you know everything about how a <em>Static Member Class</em> can access all the <code>static</code> members of the enclosing <code>class</code>, lets see how the <code>static</code> member <code>class</code> accesses the <code>non-static</code> members of the enclosing <code>class</code> on an object of the enclosing <code>class</code>.</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    int a = 10;
    private int b = 20;
    static class Inner
    {
        void display()
        {
            Outer outer = new Outer();
            System.out.println(outer.a);    //displays 10
            System.out.println(outer.b);    //displays 20
        }
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>There is no problem in the <code>Statement # 10</code> as it accesses a <code>non-private</code> member of the enclosing <code>class</code>. There is nothing wrong with it. But <code>Statement # 11</code> tries to access a <code>private</code> member of the enclosing <code>class</code>. This is how it is made possible after compilation-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code after Compilation</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
16.<br />
17.<br />
18.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    int a = 10;
    private int b = 20;
    static int access$000(Outer <strong>outer</strong>)
    {
        return <strong>outer</strong>.b;
    }
}
class Outer$Inner
{
    void display()
    {
        Outer outer = new Outer();
        System.out.println(outer.a);                 //displays 10
        System.out.println(Outer.access$000(outer)); //displays 20
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>In this case the compiler generates a <code>static</code> package visible method at <code>Statement # 5</code> which takes an object of the enclosing <code>class</code> as parameter. <code>Statement # 16</code> accesses the <code>private non-static</code> member of the enclosing <code>class</code> by calling this method and sending the instance of the enclosing <code>class</code> as argument. The method returns the value of the field on the object passed to it.<br />
Till now you have only seen how to <strong>access</strong> the value of fields from an <em>Inner Class</em>. But you can also <strong>mutate</strong> i.e. set the value of fields and make method calls. The example below shows you how this happens for <code>non-private</code> members-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd" valign="top"><code><br />1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
16.<br />
17.<br />
18.<br />
19.<br />
20.<br />
21.<br />
22.<br />
23.<br />
24.<br />
25.<br />
26.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code>
<pre>class Outer
{
    int a = 10;
    static int b = 20;
    int getProduct()
    {
        return a * b;
    }
    static int getCube()
    {
        return b * b * b;
    }
    static class Inner
    {
        void display()
        {
            b = 2;
            System.out.println(b);                  //displays 2
            System.out.println(getCube());              //displays 8
            Outer outer = new Outer();</code>
            outer.a = 100;
            System.out.println(outer.getProduct());     //displays 200
            System.out.println(outer.a);            //displays 100
        }
    }
}</pre>
<p></code><br />
</code></td>
</tr>
</tbody>
</table>
<p>There is not much changes made in this code by the compiler. So the code after compilation would look as-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code after Compilation</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd" valign="top"><code><br />1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
16.<br />
17.<br />
18.<br />
19.<br />
20.<br />
21.<br />
22.<br />
23.<br />
24.<br />
25.<br />
26.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code>
<pre>class Outer
{
    int a = 10;
    static int b = 20;
    int getProduct()
    {
        return a * b;
    }
    static int getCube()
    {
    return b * b * b;
    }
}
class Outer$Inner
{
    void display()
    {
        Outer.b = 2;
        System.out.println(Outer.b);                //displays 2
        System.out.println(Outer.getCube());    //displays 8
        Outer outer = new Outer();
        outer.a = 100;
        System.out.println(outer.getProduct()); //displays 200
        System.out.println(outer.a);                //displays 100
    }
}</pre>
<p></code><br />
</code></td>
</tr>
</tbody>
</table>
<p>The compiler adds the name of the <code>class Outer</code> before the field and method names at <code>Statements # 18, 19</code> and <code>20</code>. This is the only change made by the compiler (apart from making the <code>class Inner</code> a top level class). Its easy enough! But if what we are trying to access is <code>private</code> member of the enclosing class, then the code becomes a bit complicated. Lets see how you can mutate the values of <code>private</code> fields of the enclosing class-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    private int a = 10;
    private static int b = 20;
    static class Inner
    {
        void display()
        {
            b = 2;
            Outer outer = new Outer();
            outer.a = 100;
        }
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>For <code>Statement # 9</code> the compiler will generate a <code>static</code> package visible method that takes the value that you set with the assignment. For <code>Statement # 11</code> the compiler generates another <code>static</code> package visible method that takes the object of the <code>class Outer</code> and the value that you assign to the field. These methods are shown at <code>Statements # 5</code> and <code>9</code> in the code below-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code after Compilation</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
16.<br />
17.<br />
18.<br />
19.<br />
20.<br />
21.<br />
22.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    private int a = 10;
    private static int b = 20;
    static void access$000(int val)
    {
        b = val;
    }
    static void access$100(Outer <strong>outer</strong>, int val)
    {
        <strong>outer</strong>.a = val;
    }
}
class Outer$Inner
{
    void display()
    {
        Outer.access$000(2);              //b = 2;
        Outer outer = new Outer();
        Outer.access$100(outer, 100);     //outer.a = 100;
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>The statements at <code>Statements # 18</code> and <code>20</code> are generated by the compiler. The original statements are shown in comments. Now lets see how you can call private methods of the enclosing class-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd" valign="top"><code><br />1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
16.<br />
17.<br />
18.<br />
19.<br />
20.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    private void getProduct(int x, int y)
    {
        System.out.println(x * y);
    }
    private static int getCube(int x)
    {
        return x * x * x;
    }
    static class Inner
    {
        void display()
        {
            System.out.println(getCube(3));    //displays 9
            Outer outer = new Outer();
            outer.getProduct(10,20);           //displays 200
        }
    }
}</pre>
<p></code><br />
</code></td>
</tr>
</tbody>
</table>
<p>For both these methods, the compiler generates <code>static</code> package visible methods which inturn calls these methods which are unavailable to the <em>inner class</em> after compilation. The methods generated by the compiler for the mehods and their call are shown in the code below-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code after Compilation</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" valign="top" bgcolor="#cdcdcd"><code><br />1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
16.<br />
17.<br />
18.<br />
19.<br />
20.<br />
21.<br />
22.<br />
23.<br />
24.<br />
25.<br />
26.<br />
27.<br />
28.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    private void getProduct(int x, int y)
    {
        System.out.println(x * y);
    }
    private static int getCube(int x)
    {
        return b * b * b;
    }
    static int access$000(int x)
    {
        return getCube(x);
    }
    static void access$100(Outer <strong>outer</strong>, int x, int y)
    {
        <strong>outer</strong>.getProduct(x, y);
    }
}
class Outer$Inner
{
    void display()
    {
        System.out.println(Outer.access$000(3)); //System.out.println(getCube(3));
        Outer outer = new Outer();
        Outer.access$100(outer, 10, 20);         //outer.getProduct(10, 20);
    }
}</pre>
<p></code><br />
</code></td>
</tr>
</tbody>
</table>
<p>The methods at <code>Statements # 11</code> and <code>15</code> call the methods <code>getCube()</code> and <code>getProduct()</code> respectively. The methods calls at <code>Statements # 24</code> and <code>27</code> are generated by the compiler. The original calls are shown in comments. Lets see the opposite of this i.e. accessing the <code>private</code> members of the <em>inner class</em> from the members of the enclosing class.</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd" valign="top"><code><br />1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
16.<br />
17.<br />
18.<br />
19.<br />
20.<br />
21.<br />
22.<br />
23.<br />
24.<br />
25.<br />
26.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code>
<pre>class Outer
{
    static class Inner
    {
        static int staticVal = 10;
        int nonPrivateVal = 20;
        private int privateVal = 30;
        private void nonPrivateDisplay()
        {
            System.out.println("Non-Private Display!");
        }
        private void privateDisplay()
        {
            System.out.println("Private Display!");
        }
    }
    void display()
    {
            System.out.println(Inner.staticVal);    //displays 10
            Inner in = new Inner();
            System.out.println(in.nonPrivateVal);   //displays 20
            System.out.println(in.privateVal);      //displays 30
            in.nonPrivateDisplay();             //displays Non-Private Display!
            in.privateDisplay();                    //displays Private Display!
    }
}</pre>
<p></code></code><br />
</code></td>
</tr>
</tbody>
</table>
<p>The compiler will generate package visible methods for the access statements at <code>Statements # 22</code> and <code>24</code>. So the code would look like this-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code after Compilation</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
12.<br />
13.<br />
14.<br />
15.<br />
16.<br />
17.<br />
18.<br />
19.<br />
20.<br />
21.<br />
22.<br />
23.<br />
24.<br />
25.<br />
26.<br />
27.<br />
28.<br />
29.<br />
30.<br />
31.<br />
32.<br />
33.<br />
34.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    void display()
    {
        System.out.println(Inner.staticVal);            //displays 10
        <strong>Outer.Inner</strong> in = new <strong>Outer.Inner</strong>();
        System.out.println(in.nonPrivateVal);           //displays 20
        System.out.println(<strong>Outer.Inner</strong>.access$000(in)); //displays 30
        in.nonPrivateDisplay();                             //displays Non-Private Display!
        <strong>Outer.Inner</strong>.access$100(in);                     //displays Private Display!
    }
}
class Outer$Inner
{
    static int staticVal = 10;
    int nonPrivateVal = 20;
    private int privateVal = 30;
    private void nonPrivateDisplay()
    {
        System.out.println("Non-Private Display!");
    }
    private void privateDisplay()
    {
        System.out.println("Private Display!");
    }
    static int access$000(Outer outer)
    {
        return outer.privateVal;
    }
    static void access$100(Outer outer)
    {
        outer.privateDisplay();
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>Note that I have used the name <code>Outer.Inner</code> to represent the <em>inner class</em> in the enclosing class at <code>Statements # 6, 8</code> and <code>10</code>. This is just to avoid any confusion as the name <code>Outer.Inner</code> must be used to represent the <em>inner class</em> from any code outside the enclosing class. There is one last thing that you must note before we move to <em>Non Static Member Classes</em>. If you don't access some members of the enclosing class from the <em>inner class</em> then the compiler doesn't generates methods for them. For Example-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    static int a = 10;
    static class Inner
    {
        void display()
        {
            System.out.println("Hello!");
        }
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>The code above will not be altered much by the compiler. Since the field <code>a</code> is not accessed in the <em>inner class</em> so no method will be generated for that. So the code after compilation will look like this-</p>
<table style="border-collapse:collapse;" border="1" width="100%">
<thead>
<tr>
<th colspan="2" align="left" bgcolor="#CCCCCC">Code after Compilation</th>
</tr>
</thead>
<tbody>
<tr>
<td width="25" bgcolor="#cdcdcd"><code>1.<br />
2.<br />
3.<br />
4.<br />
5.<br />
6.<br />
7.<br />
8.<br />
9.<br />
10.<br />
11.<br />
</code></td>
<td valign="top" bgcolor="#eeeeee"><code></p>
<pre>class Outer
{
    private static int a = 10;
}
class Outer$Inner
{
    void display()
    {
        System.out.println("Hello!");
    }
}</pre>
<p></code></td>
</tr>
</tbody>
</table>
<p>I think now you know about Static Member classes from inside out. I hope this will help you a lot. View my other posts to learn more about the other forms of Inner Classes.</p>
<p>The next recommended post is about Non-Static Member classes which you can view at <a href="http://jgenius.blogspot.com">jgenius.blogspot.com</a><a href="http://jgenius.blogspot.com/2008/09/static-inner-classes.html"></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ankitgarg.wordpress.com/34/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ankitgarg.wordpress.com/34/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ankitgarg.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ankitgarg.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ankitgarg.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ankitgarg.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ankitgarg.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ankitgarg.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ankitgarg.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ankitgarg.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ankitgarg.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ankitgarg.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ankitgarg.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ankitgarg.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ankitgarg.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ankitgarg.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ankitgarg.wordpress.com&amp;blog=4563241&amp;post=34&amp;subd=ankitgarg&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ankitgarg.wordpress.com/2008/08/21/static-member-classes-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cac347354d60dc0cdf88b2ea8ef9801b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">ankitgarg</media:title>
		</media:content>
	</item>
	</channel>
</rss>
