Liang Chapter 6 Variable scope and the keyword this.

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

Class Inheritance Liang, Chpt 8. X is a Y: the is-a hierarchy life-form animal person clown geometric object circle cylinder instrument trumpet piano.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Access to Names Namespaces, Scopes, Access privileges.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
More on Objects CS 102 More, more, more on Objects.
Review 4 View classes as modules Encapsulate operations Functions are static methods 4 View classes as struct types Encapsulate data 4 View classes as.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
OOPDA Review. Terminology class-A template defining state and behavior class-A template defining state and behavior object-An instance of a class object-An.
Objects and Classes II Chapter 6, continued. Objects can be parameters Methods take parameters: e.g. Math.sqrt(3.0); takes 3.0 as a parameter Objects.
Introduction to Programming with Java, for Beginners Scope.
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 9 Objects and Classes
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Introduction to Objects A way to create our own types.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
Agenda Scope of variables Comparable interface Location class in GridWorld homework.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Introduction to Object-oriented Programming
Lecture 3: Introduction to Object and Classes
Namespaces, Scopes, Access privileges
Building Java Programs
Classes & Objects: Examples
Local Variables, Global Variables and Variable Scope
Static in Classes CSCE 121 J. Michael Moore.
Simple Classes in Java CSCI 392 Classes – Part 1.
Objects and Classes Creating Objects and Object Reference Variables
Namespaces, Scopes, Access privileges
Scope of variables class scopeofvars {
Java Basics II Minseok Kwon
Handout-4b More on Classes
Chapter 6 Objects and Classes
Chapter 9 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

Liang Chapter 6 Variable scope and the keyword this

Variable scope A local variable is visible only within the block for which it is declared Inside that block, its name is unique—you may not declare another variable of the same name within that block It's scope is the block within which it is declared

Examples of local variable issues for(int i=0; i<5; i++) { x += i; } System.out.println(i);// oops The variable i only exists within the for-loop

Examples of local variables A variable declared in a method is local to that method public static String repeatNString(String s, int i){ String result = “”; for (int x = 0; x < I; x++){ result = result + s; } return result; } The result variable can only be used within the repeatNString method!

Another example This, however, is OK public void goodMethod() { int x; int y; for (int i=0; i<5; i++) { x += i; } for (int i=0; i<5; i++) { y += i; }

Global variables An instance variable can be accessed by any method within that class Instance variables are global variables It is possible to declare a local variable with the same name as a global variable If you do....the global variable is hidden! The name will refer to the local variable, and you will probably be confused

Global variables: example 1 // instance variable declared at the end class Circle { double findArea() { return radius*radius*Math.PI; } double radius = 1.0; }

Example 2: care required! class Foo { int x = 0; int y = 0; void aMethod() { int x = 1; System.out.println("x is " + x); System.out.println("y is " + y); } This will refer to the x inside the method, not the one outside: it will print out 1, not 0.

More name clashes..... If a method-local variable and a global (instance) variable have the same name, only the local variable is visible The instance-level variable is hidden The instance variable can still be accessed using the keyword this

Using this public class TestClass{ public int i = 5; void setI(int i){ this.i= i; } … // methods constructors etc. } To refer to the variable belonging to the object from within the object, we can’t use the variable name of the object. Instead, we use the java keyword this. This means “this object”. Suppose we have a class called TestClass, containing an instance variable i. We can refer to this i variable for instances of the class: TestClass testObject = new TestClass(); System.out.println(testObject.i); Suppose the following TestClass definition. Then testObject.i refers to the instance-level variable i in the object testObject. this.i means “the i in this object”: in other words, the instance variable i.

Using this in a constructor public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } this.radius is the radius in the object; radius is the variable in the constructor method

this has two uses! this.x refers to instance var x of the present object. this() calls the default constructor for an object. This is sometimes handy: it allows complicated constructors to call the default constructor. public class Circle{ private static int NumOfCircles = 0; private double radius; public Circle(){ this.radius = 0.0; NumOfCircles++; } public Circle(double radius){ this(); this.radius = radius; } this() calls the default constructor (which does the NumOfCircles counting). Changes the radius in the created object.