Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

31
n to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak

Transcript of Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Page 1: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Introduction to the “this”

reserved word

Java - Supplemented Learning

By: Keenan Ratushniak

Page 2: Introduction to the “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.

Page 3: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

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

Page 4: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

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 don’t 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++)

Page 5: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

When to use “this”At 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

Page 6: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

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

Page 7: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

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 the “this” reference is used and not used.

Page 8: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

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

Page 9: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Common Programming Practice

public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String newName){

name = newName;

}

}

Page 10: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Common Programming Practice

public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String newName){

name = newName;

}

}

Local Variable

Page 11: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Common Programming Practice

public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String newName){

name = newName;

}

}

Local Variable

Assignment From Local to Instance Data

Page 12: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Common Programming Practice

public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String newName){

name = newName;

}

}

Local Variable

Assignment From Local to Instance Data

Instance Data now has Value from Constructor.

Page 13: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Common Programming Practice

public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String newName){

name = newName;

}

}

Local Variable

Assignment From Local to Instance Data

Instance Data now has Value from Constructor

Compiler Understands

Page 14: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

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

Page 15: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Attempt Without Using “This”

public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String name){

name = name;

}

}

Page 16: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Attempt Without Using “This”

public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String name){

name = name;

}

}

Local Variable?

Page 17: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Attempt Without Using “This”

public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String name){

name = name;

}

}

Local Variable?Local Variable?

Page 18: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Attempt Without Using “This”

public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String name){

name = name;

}

}

Local Variable?Local Variable?

OR instance data??

Page 19: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Attempt Without Using “This”

public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String name){

name = name;

}

}

Local Variable?Local Variable?

OR instance data??

OR instance data??

Page 20: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Attempt Without Using “This”

public 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 Confused

Page 21: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

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

Page 22: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

How to apply “this”public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String name){

this.name = name;

}

}

Page 23: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

How to apply “this”public class Contact{

//Instance Data

String name;

//Constructor

public Contact(String name){

this.name = name;

}

}

Local Variable

Page 24: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

How to apply “this”public 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

Page 25: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

How to apply “this”public 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 referring to this class instance data variable (the class level)

Page 26: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

How to apply “this”public 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 referring to this class instance data variable (the class level)

Compiler Understands

Page 27: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

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

Page 28: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Applying to MethodsThis is the common practice way of coding like with the constructor

public class SphereEnhanced{

//Instance Data

private double diameter;

//Constructor

public SphereEnhanced(double diameterInit){diameter = diameterInit;

}

//Method to set diameter

public double setDiameter(double newDiameter){

diameter = newDiameter;}

Page 29: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Applying to MethodsUsing the this reference for the method

public class SphereEnhanced{

//Instance Data

private double diameter;

//Constructor

public SphereEnhanced(double diameterInit){diameter = diameterInit;

}

//Method to set diameter

public double setDiameter(double diameter){

this.diameter = diameter;}

Page 30: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Applying to MethodsUsing the this reference for the method and constructor

public class SphereEnhanced{

//Instance Data

private double diameter;

//Constructor

public SphereEnhanced(double diameter){this.diameter = diameter;}

//Method to set diameter

public double setDiameter(double diameter){this.diameter = diameter;}

Page 31: Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.

Summary• Using the this reference isn’t 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.