<?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>Smartkey - Java Software Consultancy &#187; Beginner</title>
	<atom:link href="http://blog.smartkey.co.uk/tag/beginner/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smartkey.co.uk</link>
	<description></description>
	<lastBuildDate>Tue, 13 Dec 2011 15:03:46 +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>An Explanation of &#8216;this&#8217; in Java</title>
		<link>http://blog.smartkey.co.uk/2009/11/what-is-this/</link>
		<comments>http://blog.smartkey.co.uk/2009/11/what-is-this/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 19:03:42 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java Programming]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=478</guid>
		<description><![CDATA[As every Java programmer should know, code within any non-static method can access the current object via a special reference called 'this'. Just how this works may not be immediately obvious so I've taken a moment to explain it here.
]]></description>
			<content:encoded><![CDATA[<p>As every Java programmer should know, code within any non-static method can access the current object via a special reference called &#8216;this&#8217;. Just how this works may not be immediately obvious so I&#8217;ve taken a moment to explain it here.</p>
<p>The best way I know to explain &#8216;this&#8217; properly is by taking a look at how object oriented languages actually work and comparing them to their older siblings: structured programming languages. </p>
<p>In a structured programming language (like C or Pascal for example) you store data in a struct or a record and reuse code by creating functions. Moreover, when you call a function you may pass them records to work with.</p>
<p>So before we start looking at Java, let&#8217;s consider some pseudo-code in an imaginary structured programming language. First we&#8217;ll define an account record structure in this language, and then a simple function that can be used to update them:</p>
<pre class="brush: php">
//define an account record structure with two fields (balance and overdraft)
record account {
    int balance;
    int overdraft;
}

//now define a function for updating the account&#039;s balance
void deposit(account a, int amount) {
    a.balance += amount;
}
</pre>
<p>So, given an account record &#8216;a&#8217;, we could use our imaginary language to deposit £100 by calling the above function as follows:</p>
<pre class="brush: php">
//create a new account record
a = new account();

//update the account&#039;s balance using the function
deposit(a, 100);
</pre>
<p>In an Object Oriented language like Java we would more likely define an account class with a deposit method:</p>
<pre class="brush: java">
public class Account {
    private int balance;

    public void deposit(int amount) {
        balance += amount;
    }
}
</pre>
<p>and use it like this:</p>
<pre class="brush: java">
Account a = new Account();
a.deposit(100);
</pre>
<p>As we know, Object Orientation is just a higher level programming abstraction. Therefore, the code that you&#8217;d write in Java is just another way of expressing what you&#8217;d write in a procedural language.</p>
<p>In fact, the Java compiler actually produces bytecode that looks more like procedural code. For example, a method call like this:</p>
<pre class="brush: java">
a.deposit(100)
</pre>
<p>would actually be compiled as though it were a function call:</p>
<pre class="brush: java">
deposit(a, 100);
</pre>
<p>and our deposit method would be treated likewise and compiled as though it were:</p>
<pre class="brush: java">
void deposit(Account this, int amount) {
    this.balance += amount;
}
</pre>
<p>Note that the compiler has restructured the statement as a function and introduced an initial parameter called &#8216;this&#8217; that will be a reference to the object that the method is being invoked on. As you can see then, it is this &#8216;this&#8217; variable that allows you to refer explicitly to the current object.</p>
<p>The one gotcha here is if you define a method with the static modifier. Static methods don&#8217;t require that an object is created before they are called; they are generally just called via the class name, like Math.min(&#8230;) for example. In this sense they really are just like a procedural language function so the compiler does not introduce a &#8216;this&#8217; parameter. Clearly then, this is why you cannot access &#8216;this&#8217; from within static methods.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2009/11/what-is-this/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

