Introduction to Programming with Java, for Beginners Scope.

Slides:



Advertisements
Similar presentations
1.00 Lecture 11 Scope and Access. Variable Lifecycles Instance (or object) variables – Created when their containing object is created – Initialized to.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Programming G51PRG University of Nottingham Revision 3 Essam Eliwa.
Written by: Dr. JJ Shepherd
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann.
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)
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Classes and Objects Systems Programming.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Liang Chapter 6 Variable scope and the keyword this.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
CS180 Chapter 4 2/1/08. Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters.
30-Jun-15 Static Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
COMP More About Classes Yi Hong May 22, 2015.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
1 Storage Classes, Scope, and Recursion Lecture 6.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
CSC 212 Object-Oriented Programming and Java Part 2.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
01/24/2005 Introduction to Programming with Java, for Beginners A Closer Look at Constructors and Methods.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
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.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
3 Introduction to Classes and Objects.
C Functions -Continue…-.
Yanal Alahmad Java Workshop Yanal Alahmad
Static Methods 14-Nov-18.
Defining Classes and Methods
CHAPTER 6 GENERAL-PURPOSE METHODS
Workshop for Programming And Systems Management Teachers
Topics to cover Instance variables vs. local variables.
Classes and Objects Systems Programming.
Methods Scope How are names handled?
Day 11 The Last Week!.
Presentation transcript:

Introduction to Programming with Java, for Beginners Scope

1  Scope means the area of code in which an entity is known  We will discuss the scope of a:  Variable - what code can access it?  Method - what code can call it?  Sometimes scope is explicitly designated with a keyword  private: known only within the class  public: known outside of (and within) the class  Other times it is implicitly designated by location  e.g. method parameters are known only in the method in which they are defined

2 Instance Variables, Methods Recommendations  Make all instance variables private  Make most methods public  Make a method private if it need/should only be called by methods in the same class A common scenario:  We write a method that gets very long and/or complicated, so we want to break it in to subproblems  Or, we have several methods that need to do some of the same subproblem  We create a private “helper” method to handle the subproblem and let the other methods call it.

3 Method Parameters A method parameter is an “input variable” Scope: the method in which it is defined It “comes alive” when the method is entered It “dies” when the method is exited No other method can access (read/write) it

4 Local Variables A “local variable” is defined within a method body {} It may be defined in a block {} within a method body Scope: point of declaration to end of closest enclosing block We don’t use public/private for local variables. They are inherently private to the method in which they are defined

5 CodeNotes // Converts 0,1,2,3 to // “north”, “east”, “south”, “west” public String direction(int d){ String result = “unknown”; if (d == 0) result = "north"; else if (d == 1) result = "east"; else if (d == 2) result = ”south"; else if (d == 3) result = "west"; return result; } Method parameters (e.g. d) are only known within the method. Local variables (e.g. result), are known from the point of declaration until the end curly brace of the block in which they are declared. By contrast, instance variables are declared outside of any method and are known by all methods in the class in which they’re declared. Example

6 The “this” Keyword The keyword this means “this object”. It may be used to specify an instance variable public class Dot{ private int x; public void setX(int x){ this.x = x; // fixed!! } } public class Dot{ private int x; public void setX(int x){ x = x; // problem!! } }