Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak."— Presentation transcript:

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

2 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.

3 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

4 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++)

5 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

6 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

7 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.

8 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

9 Common Programming Practice public class Contact{ //Instance Data String name; //Constructor public Contact(String newName){ name = newName; }

10 Common Programming Practice public class Contact{ //Instance Data String name; //Constructor public Contact(String newName){ name = newName; } Local Variable

11 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

12 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.

13 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

14 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

15 Attempt Without Using “This” public class Contact{ //Instance Data String name; //Constructor public Contact(String name){ name = name; }

16 Attempt Without Using “This” public class Contact{ //Instance Data String name; //Constructor public Contact(String name){ name = name; } Local Variable?

17 Attempt Without Using “This” public class Contact{ //Instance Data String name; //Constructor public Contact(String name){ name = name; } Local Variable?

18 Attempt Without Using “This” public class Contact{ //Instance Data String name; //Constructor public Contact(String name){ name = name; } Local Variable? OR instance data??

19 Attempt Without Using “This” public class Contact{ //Instance Data String name; //Constructor public Contact(String name){ name = name; } Local Variable? OR instance data??

20 Attempt Without Using “This” public class Contact{ //Instance Data String name; //Constructor public Contact(String name){ name = name; } Local Variable? OR instance data?? Compiler is Confused

21 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

22 How to apply “this” public class Contact{ //Instance Data String name; //Constructor public Contact(String name){ this.name = name; }

23 How to apply “this” public class Contact{ //Instance Data String name; //Constructor public Contact(String name){ this.name = name; } Local Variable

24 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

25 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)

26 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

27 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

28 Applying to Methods This 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; }

29 Applying to Methods Using 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; }

30 Applying to Methods Using 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; }

31 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.


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

Similar presentations


Ads by Google