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

Slides:



Advertisements
Similar presentations
Fields, Constructors, Methods
Advertisements

Written by: Dr. JJ Shepherd
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
Games and Simulations O-O Programming in Java The Walker School
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Writing Classes (Chapter 4)
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
4.1 Instance Variables, Constructors, and Methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Functions, Objects, and Programming IDIA 619 Spring 2014 Bridget M. Blodgett.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
10-Nov-15 Java Object Oriented Programming What is it?
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CSC 142 Computer Science II Zhen Jiang West Chester University
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
9.1 Class (static) Variables and Methods
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Haidong Xue Summer 2011, at GSU
Chapter 3: Using Methods, Classes, and Objects
Using local variable without initialization is an error.
CS 302 Week 11 Jim Williams, PhD.
Interfaces.
CSC240 Computer Science III
Classes & Objects: Examples
Classes, Encapsulation, Methods and Constructors (Continued)
Building Java Programs
Building Java Programs
Dr. R Z Khan Handout-3 Classes
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Topics to cover Instance variables vs. local variables.
Corresponds with Chapter 5
Threads and concurrency / Safety
Presentation transcript:

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.

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

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

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 the “this” 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 Practice public class Contact{ //Instance Data String name; //Constructor public Contact(String newName){ name = newName; }

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

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

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.

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

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 “This” public class Contact{ //Instance Data String name; //Constructor public Contact(String name){ name = name; }

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

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

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

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

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

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 “this” public class Contact{ //Instance Data String name; //Constructor public Contact(String name){ this.name = name; }

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

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

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)

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

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 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; }

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; }

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; }

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.