<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alessandro Stella - Appunti di informatica &#187; inner class</title>
	<atom:link href="http://www.alessandrostella.it/tag/inner-class/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alessandrostella.it</link>
	<description>Appunti sparsi di informatica</description>
	<lastBuildDate>Tue, 07 Feb 2012 17:20:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Java, anonymous classes (classi anonime)</title>
		<link>http://www.alessandrostella.it/programmazione/2010/04/java-anonymous-classes/</link>
		<comments>http://www.alessandrostella.it/programmazione/2010/04/java-anonymous-classes/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 16:02:58 +0000</pubDate>
		<dc:creator>Alessandro</dc:creator>
				<category><![CDATA[programmazione]]></category>
		<category><![CDATA[anonymous class]]></category>
		<category><![CDATA[inner class]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.alessandrostella.it/?p=925</guid>
		<description><![CDATA[Un&#8217;anonymous class (classe anonima) è una inner class che viene contemporaneamente definita e istanziata e non ha un nome. Un&#8217;anonymous class (classe anonima) esiste se e soltanto se esiste una super classe da estendere o un&#8217;interfaccia da implementare! Quando estende una classe, la sintassi dichiarativa assume la seguente forma: new &#60;nome superclasse&#62; (&#60;lista opzionale di [...]<div id="artCorr">
            <h4>Articoli correlati</h4>
        <ol>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-local-classes/" rel="bookmark">Java, local classes</a><!-- (12.2)--></li>
	    	    <li><a href="http://www.alessandrostella.it/guide-e-manuali/2010/07/jasper-report-ireport-java/" rel="bookmark">JasperReport, iReport e java</a><!-- (6)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-ciclo-di-vita-degli-oggetti-garbage-collection/" rel="bookmark">Java, ciclo di vita degli oggetti e garbage collection</a><!-- (5.6)--></li>
	            </ol>
    </div>]]></description>
			<content:encoded><![CDATA[<p><strong>Un&#8217;anonymous class (classe anonima) è una inner class che viene contemporaneamente definita e istanziata e non ha un nome</strong>.<br />
Un&#8217;anonymous class (classe anonima) esiste se e soltanto se esiste una super classe da estendere o un&#8217;interfaccia da implementare!<br />
Quando estende una classe, la sintassi dichiarativa assume la seguente forma:<br />
<strong>new &lt;<em>nome superclasse</em>&gt; (&lt;<em>lista opzionale di argomenti</em>&gt;) {&#8230;}</strong><br />
Quando invece implementa un&#8217;interfaccia, assume la seguente forma:<br />
<strong>new &lt;<em>nome interfaccia</em>&gt; () {&#8230;}</strong><br />
Bisogna osservare che sebbene una classe anonima estenda una classe o implementi un&#8217;interfaccia, essa non usa né la clausola <em>extends</em> né la clausola <em>implements</em>.<br />
Inoltre, come per le <a href=http://www.alessandrostella.it/java/2010/04/java-local-classes/">local classes</a>, anche le anomymous classes non possono usare la parola chiave <em>static</em>  in fase di dichiarazione.<br />
[to be continued...]</p>
<div id="artCorr">
            <h4>Articoli correlati</h4>
        <ol>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-local-classes/" rel="bookmark">Java, local classes</a><!-- (12.2)--></li>
	    	    <li><a href="http://www.alessandrostella.it/guide-e-manuali/2010/07/jasper-report-ireport-java/" rel="bookmark">JasperReport, iReport e java</a><!-- (6)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-ciclo-di-vita-degli-oggetti-garbage-collection/" rel="bookmark">Java, ciclo di vita degli oggetti e garbage collection</a><!-- (5.6)--></li>
	            </ol>
    </div>]]></content:encoded>
			<wfw:commentRss>http://www.alessandrostella.it/programmazione/2010/04/java-anonymous-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java, local classes</title>
		<link>http://www.alessandrostella.it/programmazione/2010/04/java-local-classes/</link>
		<comments>http://www.alessandrostella.it/programmazione/2010/04/java-local-classes/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 10:01:25 +0000</pubDate>
		<dc:creator>Alessandro</dc:creator>
				<category><![CDATA[programmazione]]></category>
		<category><![CDATA[inner class]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[local class]]></category>

		<guid isPermaLink="false">http://www.alessandrostella.it/?p=899</guid>
		<description><![CDATA[Per local class (classe locale) si intende una inner class che è definita in un blocco di codice. Un blocco di codice potrebbe essere il corpo di un metodo, di un costruttore, un blocco locale, un inizializzatore statico o un inizializzatore di istanza. Alcune caratteristiche di una local class: una local class non può avere [...]<div id="artCorr">
            <h4>Articoli correlati</h4>
        <ol>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/02/java-1-6-nested-type-e-inner-classes/" rel="bookmark">Java 1.6, nested type e inner classes</a><!-- (18.8)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-non-static-member-class/" rel="bookmark">Java, non-static member class</a><!-- (17.1)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-anonymous-classes/" rel="bookmark">Java, anonymous classes (classi anonime)</a><!-- (16.4)--></li>
	            </ol>
    </div>]]></description>
			<content:encoded><![CDATA[<p><strong>Per local class (classe locale) si intende una inner class che è definita in un blocco di codice</strong>. Un blocco di codice potrebbe essere il corpo di un metodo, di un costruttore, un blocco locale, un inizializzatore statico o un inizializzatore di istanza.<br />
Alcune caratteristiche di una local class:</p>
<ul>
<li>una local class non può avere memebri statici (questo però non esclude la possibilità di avere un membro <em>final static</em>, essendo esso una costante)</li>
<li>una local class non può avere modificatori di accessibilità (public, private, ecc.). Sarà quindi dichiarata scrivendo semplicemente <em>class nomeClasse {&#8230;}</em>
	</li>
<li>una local class può estendere un&#8217;altra classe</li>
<li>all&#8217;interno del blocco in cui è definita, una local class può accedere solo ai membri dichiarati <em>final</em></li>
<li>una non-static local class può accedere sia ai membri statici sia ai membri non statici della classe contenitore (da non confondere con il blocco contenitore)
</li>
<li>una static local class può accedere solo ai membri statici della classe che la contiene</li>
<li>una local class (essendo locale) può essere istanziata solo nel blocco nel quale è definita e deve essere dichiarata prima di essere utilizzata</li>
</ul>
<p>Prima accortezza: se il blocco contenente la dichiarazione della local class è definito in un contesto statico (ad esempio un metodo statico o un inizializzatore statico) allora la local class è implicitamente statica e quindi non richiede alcun oggetto contenitore per essere istanziata.<br />
Facciamo però attenzione al fatto che, sebbene in certi contesti (appena visti) la local class è intesa implicitamente statica, essa non consente di utilizzare nella propria dichiarazione la parola chiave <em>static</em>. Mai.<br />
Facciamo anche attenzione al fatto che se una local class si trova dichiarata in un contesto statico, ciò influenzerà quello che la local class può &#8220;vedere&#8221; nel contesto contenitore.<span id="more-899"></span><br />
Possiamo leggere i commenti nel seguente esempio per chiarirci (spero) le idee&#8230;</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EsempioLocalClass<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> varNonStatica <span style="color: #339933;">=</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">;</span> <br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> metodoNonStatico <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> flv <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> hlv <span style="color: #339933;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> nflv <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">class</span> localClassNonStatica <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// dichiarazione local class. siamo nel metodo &quot;metodoNonStatico&quot;, non dimentichiamolo</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> f1<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// errore! Non si possono dichiarare variabili static non final in una local class</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> f2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// va bene. Varibile final static consentita in una local class</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> f3 <span style="color: #339933;">=</span> param<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// tutto ok. Abbiamo visibilità della costante param</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> f4 <span style="color: #339933;">=</span> flv<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// ok. flv appartiene al metodo contenitore ad è final</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> f5 <span style="color: #339933;">=</span> nflv<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// errore! Del blocco contenitore possiamo &quot;vedere&quot; solo le variabili definite &quot;final&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> f6 <span style="color: #339933;">=</span> varNonStatica<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// ok. varNonStatica è dichiarata nella Top Level Class, quindi è visibile nella local class</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> metodoStatico <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// attenzione! Questo è un metodo statico... quindi?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> flv <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> hlv <span style="color: #339933;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> nflv <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">class</span> localClassImplicitamenteStatica <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> f1<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// errore! Repetita iuvant: niente static non final in una local class</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> f2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// va bene invece una final static</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> f3 <span style="color: #339933;">=</span> param<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// ok. param appartine al metodo contenitore ed è final</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> f4 <span style="color: #339933;">=</span> flv<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// ok. flv appartine al metodo contenitore ed è final</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> f5 <span style="color: #339933;">=</span> nflv<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// errore! nflv non è final</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> f6 <span style="color: #339933;">=</span> varNonStatica<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// errore! Ora la nostra local class è implicitamente statica e quindi non abbiamo accesso alle variabili della Top Level Class (a meno che non siano dichiarate static)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div id="artCorr">
            <h4>Articoli correlati</h4>
        <ol>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/02/java-1-6-nested-type-e-inner-classes/" rel="bookmark">Java 1.6, nested type e inner classes</a><!-- (18.8)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-non-static-member-class/" rel="bookmark">Java, non-static member class</a><!-- (17.1)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-anonymous-classes/" rel="bookmark">Java, anonymous classes (classi anonime)</a><!-- (16.4)--></li>
	            </ol>
    </div>]]></content:encoded>
			<wfw:commentRss>http://www.alessandrostella.it/programmazione/2010/04/java-local-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java, non-static member class</title>
		<link>http://www.alessandrostella.it/programmazione/2010/04/java-non-static-member-class/</link>
		<comments>http://www.alessandrostella.it/programmazione/2010/04/java-non-static-member-class/#comments</comments>
		<pubDate>Sun, 04 Apr 2010 09:36:42 +0000</pubDate>
		<dc:creator>Alessandro</dc:creator>
				<category><![CDATA[programmazione]]></category>
		<category><![CDATA[inner class]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.alessandrostella.it/?p=883</guid>
		<description><![CDATA[Una non-static member class è una inner class definita senza la keyword static (vedere il punto (2) dell&#8217;esempio sotto). Bisogna osservare che: Un&#8217;istanza di una non-static member class può esistere solo e soltanto insieme all&#8217;istanza della classe che la contiene Una non-static member class non può avere static members (a meno che non siano anche [...]<div id="artCorr">
            <h4>Articoli correlati</h4>
        <ol>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/02/java-1-6-nested-type-e-inner-classes/" rel="bookmark">Java 1.6, nested type e inner classes</a><!-- (17.2)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-local-classes/" rel="bookmark">Java, local classes</a><!-- (15)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-anonymous-classes/" rel="bookmark">Java, anonymous classes (classi anonime)</a><!-- (12.8)--></li>
	            </ol>
    </div>]]></description>
			<content:encoded><![CDATA[<p><strong>Una non-static member class è una inner class definita senza la keyword <em>static</em></strong> (vedere il punto (2) dell&#8217;esempio sotto).<br />
Bisogna osservare che:</p>
<ul>
<li>Un&#8217;istanza di una non-static member class può esistere solo e soltanto insieme all&#8217;istanza della classe che la contiene
</li>
<li>Una non-static member class non può avere static members (a meno che non siano anche <em>final</em>)</li>
<li>Il codice scritto in una non-static member class può avere accesso diretto a ogni membro (inclusi i nested) anche se dichiarato <em>private</em>
</li>
</ul>
<p>Vediamo un esempio con un po&#8217; di casi particolari.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyLinkedList <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Shine the light&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// (1)</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Node makeInstance <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> info, Node next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Node<span style="color: #009900;">&#40;</span>info, next<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Node <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// (2) non-static member class (NSMC)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> maxNumOfNodes <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> nodeInfo<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Node next<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> message <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Messaggio dal Nodo &quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// (3)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Node<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nodeInfo, Node next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nodeInfo</span> <span style="color: #339933;">=</span> nodeInfo<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">next</span> <span style="color: #339933;">=</span> next<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Modifica del message di Node&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// (4)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> MyLinkedList.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">message</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; in &quot;</span> <span style="color: #339933;">+</span> nodeInfo <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (&quot;</span> <span style="color: #339933;">+</span> maxNumOfNodes <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// (5)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ListClient <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; MyLinkedList list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyLinkedList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; MyLinkedList.<span style="color: #006633;">Node</span> node1 <span style="color: #339933;">=</span> list.<span style="color: #006633;">makeInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;node1&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; MyLinkedList.<span style="color: #006633;">Node</span> node2 <span style="color: #339933;">=</span> list.<span style="color: #006633;">makeInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;node2&quot;</span>, node1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>node2.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Osserviamo il punto (4).<br />
Viene modificato il valore asseganto all&#8217;attributo di tipo stringa <em>message</em>. Ma esistono 2 attributi di tipo stringa denominati <em>message</em>! Li vediamo dichiarati al punto (1) e al punto (3), entrambi <em>private</em>. Il <em>message</em> al punto (1) appartiene alla classe contenitore (MyLinkedList), mentre quello dichiarato al punto (4) appartiene alla non-static inner class (Node). Abbiamo detto che in una non-static member class possiamo riferirci a qualunque membro, anche dichiarato <em>private</em>, anche appartenente alla classe contenitore. Ma come possiamo riferisci all&#8217;attributo message del contenitore? Lo si può fare con un uso particolare della parole chiave <em>this</em>. Osserviamo a tal riguardo il punto (5).</p>
<p>ATTENZIONE.<br />
Se non fosse stato dichiarato l&#8217;attributo al punto (4), allora scrivendo</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">return</span> message <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; in &quot;</span> <span style="color: #339933;">+</span> nodeInfo <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (&quot;</span> <span style="color: #339933;">+</span> maxNumOfNodes <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// (5a)</span></div></td></tr></tbody></table></div>
<p>non si avrebbe alcun errore (come invece ci si potrebbe aspettare), poiché ci riferiremmo, in modo implicito, all&#8217;attributo <em>message</em> della classe contenitore!!!<br />
Facciamo attenzione a questo caso particolare!</p>
<div id="artCorr">
            <h4>Articoli correlati</h4>
        <ol>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/02/java-1-6-nested-type-e-inner-classes/" rel="bookmark">Java 1.6, nested type e inner classes</a><!-- (17.2)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-local-classes/" rel="bookmark">Java, local classes</a><!-- (15)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-anonymous-classes/" rel="bookmark">Java, anonymous classes (classi anonime)</a><!-- (12.8)--></li>
	            </ol>
    </div>]]></content:encoded>
			<wfw:commentRss>http://www.alessandrostella.it/programmazione/2010/04/java-non-static-member-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java 1.6, nested type e inner classes</title>
		<link>http://www.alessandrostella.it/programmazione/2010/02/java-1-6-nested-type-e-inner-classes/</link>
		<comments>http://www.alessandrostella.it/programmazione/2010/02/java-1-6-nested-type-e-inner-classes/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 16:13:29 +0000</pubDate>
		<dc:creator>Alessandro</dc:creator>
				<category><![CDATA[programmazione]]></category>
		<category><![CDATA[anonymous class]]></category>
		<category><![CDATA[inner class]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[nested class]]></category>

		<guid isPermaLink="false">http://www.alessandrostella.it/?p=645</guid>
		<description><![CDATA[Che confusione! Sui tipi annidati (nested type) e la classi interne (inner classes) in giro per la rete ci sono scritte un sacco di fregnacce! Probabilmente perché la SUN, a partire dalla versione 1.4 di java, ha fatto un po&#8217; di casini con la nomenclatura, modificando il significato di alcuni termini. Comunque sia, ora proviamo [...]<div id="artCorr">
            <h4>Articoli correlati</h4>
        <ol>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-anonymous-classes/" rel="bookmark">Java, anonymous classes (classi anonime)</a><!-- (18.7)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-non-static-member-class/" rel="bookmark">Java, non-static member class</a><!-- (14.8)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-local-classes/" rel="bookmark">Java, local classes</a><!-- (13.9)--></li>
	            </ol>
    </div>]]></description>
			<content:encoded><![CDATA[<p>Che confusione!<br />
Sui tipi annidati (nested type) e la classi interne (inner classes) in giro per la rete ci sono scritte un sacco di fregnacce! Probabilmente perché la SUN, a partire dalla versione 1.4 di java, ha fatto un po&#8217; di casini con la nomenclatura, modificando il significato di alcuni termini. Comunque sia, ora proviamo a fare chiarezza.<br />
Per prima cosa mettiamo in chiaro un concetto: le definizioni in informatica si danno e si usano in inglese. Ci si può riferire al corrispondente termine italiano per meglio comprendere di cosa si parla, ma è l&#8217;inglese che rende univoco un concetto o una definizione.<br />
Torniamo quindi all&#8217;Università e alle definizioni <img src='http://www.alessandrostella.it/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Definizione:<br />
Con il termine <strong>nested type</strong> (tipi annidati) si intende indicare:</p>
<ul>
<li>nested classes</li>
<li>nested enums</li>
<li>nested interfaces</li>
</ul>
<p>Definizione:<br />
Con il termine <strong>nested class</strong> (classe annidata) si intende una classe dichiarata all&#8217;interno di un&#8217;altra classe.<br />
Allo stesso modo si definisono le nested enum e le nested interface.<br />
Ci sono 4 tipi di nested classes:</p>
<ol>
<li>static member classes, enums e interfaces</li>
<li><a href="http://www.alessandrostella.it/java/2010/04/java-non-static-member-class/">non-static member classes</a></li>
<li><a href="http://www.alessandrostella.it/java/2010/04/java-local-classes/">local classes</a></li>
<li><a href="http://www.alessandrostella.it/java/2010/04/java-anonymous-classes/">anonymous classes</a></li>
</ol>
<p>Definizione:<br />
Con il termine <strong>inner class</strong> (classe interna) si indica una qualsiasi nested class purché NON di tipo 1 (vedi definizione precedente).</p>
<p>Facciamo un esempio commentato a dovere:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> Alessandro <span style="color: #009900;">&#123;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// (1) Top level class</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> SMC <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">/*...*/</span><span style="color: #009900;">&#125;</span>&nbsp; <span style="color: #666666; font-style: italic;">// (2) Static member class</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">interface</span> SMI <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">/*...*/</span><span style="color: #009900;">&#125;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// (3) Static member interface</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">class</span> NSMC <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">/*...*/</span><span style="color: #009900;">&#125;</span>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// (4) Non-static member (inner) class</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> nsm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">class</span> NSLC <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">/*...*/</span><span style="color: #009900;">&#125;</span>&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// (5) Local (inner) class in non-static context</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> sm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">class</span> SLC <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">/*...*/</span><span style="color: #009900;">&#125;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// (6) Local (inner) class in static context</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; SMC nsf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SMC<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// (7) Anonymous (inner) class in non-static context</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*...*/</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">static</span> SMI sf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SMI<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// (8) Anonymous (inner) class in static context</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*...*/</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">enum</span> SME <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">/*...*/</span><span style="color: #009900;">&#125;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// (9) Static member enum</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div id="artCorr">
            <h4>Articoli correlati</h4>
        <ol>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-anonymous-classes/" rel="bookmark">Java, anonymous classes (classi anonime)</a><!-- (18.7)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-non-static-member-class/" rel="bookmark">Java, non-static member class</a><!-- (14.8)--></li>
	    	    <li><a href="http://www.alessandrostella.it/programmazione/2010/04/java-local-classes/" rel="bookmark">Java, local classes</a><!-- (13.9)--></li>
	            </ol>
    </div>]]></content:encoded>
			<wfw:commentRss>http://www.alessandrostella.it/programmazione/2010/02/java-1-6-nested-type-e-inner-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

