Quick Index
Access Ivar Using Keyword this


When we use the same variable name multiple times it becomes confusing.

We'll show here how to simplify and avoid gotchas.

Will the Real Boo Stand Up?
Which var is 99, which is 101? We could figure it out, but it's much easier just to avoid this complexity (see next).
public class Foo {

	private int boo;

	public void anyMethodOrConstructor() {
		int boo = 99;
		boo = 101;
	}
	
}
Use "this"
Now it is clear that 101 goes to the instance variable because "this" as we know is the instance (object) in play.
public class Foo {

	private int boo;

	public void anyMethodOrConstructor() {
		int boo = 99;
		this.boo = 101;
	}
	
}
Another Example Using "this"
Again this tells us it is the instance variable.
public class Foo {
	private int boo;

	public void anyMethodOrConstructor(int boo) {
		this.boo = boo;
	}
}
More Clarity
Renaming the method param "aBoo" provides even more clarity and the more clarity the better in this book.
public class Foo {
	private int boo;

	public void anyMethodOrConstructor(int aBoo) {
		this.boo = aBoo;
	}
}

Returning Boolean Data Type



Examples
Boolean data types are returned in the same way as other data types, as shown.
//example #1
//Note assume that method canPrint returns a boolean data type
myBooleanVariable = someObject.canPrint();
return myBooleanVariable;

//example #2/
//Note assume that method canPrint returns a boolean data type
return someObject.canPrint();

//example #3
//Note assume that method canRun returns a boolean data type
if (this.canRun())
	return true
return false;

//example #4
//Note assume that method canRun returns a boolean data type
return this.canRun()