Upload
corso101
View
225
Download
0
Embed Size (px)
DESCRIPTION
A description of how to properly use the java reserved word 'this'.
Citation preview
Introduction tothe this
reserved word
Java - Supplemented Learning
By: Keenan Ratushniak
This power point is intended for first time students
learning Java and builds on Java foundations. It is
intended to give a quick review to the java reserved
word this. It is a very early introduction to the use
and placements of it.
Class Level Variables When writing a class, your instance data, is your
class level variables
Any methods within your class can use and assign
values to these variables
They are Global to the class
Method Variables within Class Methods can accept parameters and declare variables
within its body
These variables are local to the method
In other words they dont exist outside the method,
and other methods cant access them directly
This is just like a counter variable within a for loop.for (int counter = 0; counter < max; counter++)
When to use thisAt this introduction level to java there are 2 conditions that must
be met to signal the possible use of the reserve word this
Conditions:
1. Your method must accept a parameter or declare a variable
and
2. You must CHOOSE to name your instance data variable and
method variable the same
How this works The this reserve word is used to tell the compiler
that within a method there are 2 variables of the same
name. One being your instance data variable and one
being your local method variable
You apply this to the variable that is your Instance
Data or class level variable
In this Code Snippet the this reference is not needed. The constructor uses a variable of a different name The getDiameter() Method directly uses the Instance Data
public class SphereEnhanced{
//Instance Data
private double diameter;
//Constructor
public SphereEnhanced(double diameterInit){diameter = diameterInit;
}
//Method to get diameter
public double getDiameter(){
return diameter;}
This basic construction will be used throughout this tutorial to show you when thethis reference is used and not used.
Common Programming Practice In the next example you will see a code similar to
the previous one
The use of this is not needed with common
programming
The example illustrates roughly what is
happening under the covers
Common Programming Practicepublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String newName){
name = newName;
}
}
Common Programming Practicepublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String newName){
name = newName;
}
}
Local Variable
Common Programming Practicepublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String newName){
name = newName;
}
}
Local Variable
Assignment From Local to Instance Data
Common Programming Practicepublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String newName){
name = newName;
}
}
Local Variable
Assignment From Local to Instance Data
Instance Datanow has Valuefrom Constructor.
Common Programming Practicepublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String newName){
name = newName;
}
}
Local Variable
Assignment From Local to Instance Data
Instance Datanow has Valuefrom Constructor
Compiler is Happyand Understands
Attempt Without Using This In this example modified from above the
parameter variable is the same name as the
instance data
The compiler cannot tell which parameter or
variable you are trying to apply the assignment to
Attempt Without Using Thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
name = name;
}
}
Attempt Without Using Thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
name = name;
}
}
Local Variable?
Attempt Without Using Thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
name = name;
}
}
Local Variable?Local Variable?
Attempt Without Using Thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
name = name;
}
}
Local Variable?Local Variable?
OR instance data??
Attempt Without Using Thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
name = name;
}
}
Local Variable?Local Variable?
OR instance data??
OR instance data??
Attempt Without Using Thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
name = name;
}
}
Local Variable?Local Variable?
OR instance data??
OR instance data??
Compiler is Sad andConfused
How to apply this The following code is exactly as the previous with
one slight modification
The this reserve word is added to the instance data
variable that we want to use within a method
How to apply this to a variable: this.name
How to apply thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
this.name = name;
}
}
How to apply thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
this.name = name;
}
}
Local Variable
How to apply thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
this.name = name;
}
}
Local Variable
Assignment From Local to this reference of Instance Data
How to apply thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
this.name = name;
}
}
Local Variable
Assignment From Local to this reference of Instance Data
this is referringto this classinstance datavariable (theclass level)
How to apply thispublic class Contact{
//Instance Data
String name;
//Constructor
public Contact(String name){
this.name = name;
}
}
Local Variable
Assignment From Local to this reference of Instance Data
this is referringto this classinstance datavariable (theclass level)
Compiler is Happyand Understands
Applying to Methods The same procedure is used for methods
If declaring a variable in the method with the
same name as the instance data you must use the
this reference
Applying to MethodsThis is the common practice way of coding like with the constructor
public class SphereEnhanced{
//Instance Dataprivate double diameter;
//Constructorpublic SphereEnhanced(double diameterInit){
diameter = diameterInit;}
//Method to set diameterpublic double setDiameter(double newDiameter){
diameter = newDiameter;}
Applying to MethodsUsing the this reference for the method
public class SphereEnhanced{
//Instance Dataprivate double diameter;
//Constructorpublic SphereEnhanced(double diameterInit){
diameter = diameterInit;}
//Method to set diameterpublic double setDiameter(double diameter){
this.diameter = diameter;}
Applying to MethodsUsing the this reference for the method and constructor
public class SphereEnhanced{
//Instance Dataprivate double diameter;
//Constructorpublic SphereEnhanced(double diameter){
this.diameter = diameter;}
//Method to set diameterpublic double setDiameter(double diameter){
this.diameter = diameter;}
Summary Using the this reference isnt common programming but is up
to the programmers discretion on when they want to use it
It is used when you choose to name a methods parameters or
declare variables inside its body the same as your instance data
Using this implicitly makes that variable reference your
instance data variable.