<?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>Peeks and Pokes &#187; Java</title>
	<atom:link href="http://mindwidgets.com/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://mindwidgets.com/blog</link>
	<description>Hey, what does this button do?</description>
	<lastBuildDate>Thu, 08 Mar 2012 06:19:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Getting rid of synthetic accessors when using inner classes</title>
		<link>http://mindwidgets.com/blog/getting-rid-of-synthetic-accessors-when-using-inner-classes/</link>
		<comments>http://mindwidgets.com/blog/getting-rid-of-synthetic-accessors-when-using-inner-classes/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 02:48:44 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://mindwidgets.com/blog/?p=105</guid>
		<description><![CDATA[The sample code below will generate a "Read access to enclosing field MyRunnableFactory.log is emulated by a synthetic accessor method" warning within the Runnable inner class: public class MyRunnableFactory { private final Logger log = Logger.getLogger("MyRunnableFactory"); public Runnable makeRunnable() { return new Runnable() { @Override public void run() { // the log variable below shows [...]]]></description>
			<content:encoded><![CDATA[<p>The sample code below will generate a <strong>"Read access to enclosing field MyRunnableFactory.log is emulated by a synthetic accessor method"</strong> warning within the Runnable inner class:</p>
<pre>
public class MyRunnableFactory {
	private final Logger log = Logger.getLogger("MyRunnableFactory");

	public Runnable makeRunnable() {
		return new Runnable() {
			@Override
			public void run() {
				// the log variable below shows the
				// synthetic accessor method warning
				<strong>log</strong>.info("Doing fun stuff");
			}
		};
	}
}
</pre>
<p>I've seen many posts on the web that suggest we ignore or suppress the warning, and just deal with the performance hit. A quick hack can rid you of this annoying warning, while also getting rid of the synthetic accessor (and its performance hit):</p>
<pre>
public class MyRunnableFactory {
	private final Logger log = Logger.getLogger("MyRunnableFactory");

	public Runnable makeRunnable() {
		// localize the instance field
		<strong>final Logger log = this.log;</strong>

		return new Runnable() {
			@Override
			public void run() {
				// the synthetic accessor is no longer needed!
				log.info("Doing fun stuff");
			}
		};
	}
}
</pre>
<p>Let's hope that a future version of the java compiler is smart enough to locally alias so we don't have to muddy our code <img src='http://mindwidgets.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://mindwidgets.com/blog/getting-rid-of-synthetic-accessors-when-using-inner-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

