<?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>Desk Checked &#187; Java</title>
	<atom:link href="http://www.deskchecked.com/category/languages/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.deskchecked.com</link>
	<description>Thomas Lee's programming blog</description>
	<lastBuildDate>Sun, 29 Nov 2009 13:01:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java, &#8220;this&#8221; and Inner/Anonymous Classes</title>
		<link>http://www.deskchecked.com/2007/06/12/java-this-and-inneranonymous-classes/</link>
		<comments>http://www.deskchecked.com/2007/06/12/java-this-and-inneranonymous-classes/#comments</comments>
		<pubDate>Mon, 11 Jun 2007 15:46:27 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.vector-seven.com/2007/06/12/java-this-and-inneranonymous-classes/</guid>
		<description><![CDATA[Brushing up on my Java for the first time in a while and I came across something fairly trivial but new to me. If you&#8217;re a seasoned Javanaut, you&#8217;ll have to indulge me for a second  .
When writing a method within an anonymous/inner class in Java, the &#8220;this&#8221; keyword obviously refers to the anonymous/inner [...]]]></description>
			<content:encoded><![CDATA[<p>Brushing up on my Java for the first time in a while and I came across something fairly trivial but new to me. If you&#8217;re a seasoned Javanaut, you&#8217;ll have to indulge me for a second <img src='http://www.deskchecked.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>When writing a method within an anonymous/inner class in Java, the &#8220;this&#8221; keyword obviously refers to the anonymous/inner class itself. What if we needed a reference to the instance of the outer class?</p>
<pre><code>
public class OuterJFrame extends JFrame {
    public OuterJFrame() {
        super("Outer/Inner/Anonymous Classes Test");
        JButton button = new JButton("Click me!");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                <strong>// type error! "this" refers to our ActionListener instance</strong>
                chooser.showOpenDialog(<strong>this</strong>);
            }
        });</code></pre>
<pre><code>    }
}
</code></pre>
<p>I stumbled across the answer to this little dilemma in the Java SE documentation: it is possible to refer to refer to the instance of the outer class using OuterClass.this:</p>
<pre><code>
public class OuterJFrame extends JFrame {
    public OuterJFrame() {
        super("Outer/Inner/Anonymous Classes Test");
        JButton button = new JButton("Click me!");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                <strong>// fixed! this works as expected</strong>
                chooser.showOpenDialog(<strong>OuterJFrame.this</strong>);
            }
        });</code></pre>
<pre><code>    }
}
</code></pre>
<p>Obviously this neat little syntactic tango won&#8217;t work with static inner classes, because they are not associated with instances of the outer class. Still useful, yeah?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deskchecked.com/2007/06/12/java-this-and-inneranonymous-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
