Summary of the lecture We discussed –variable scope –instance variable declarations –variable lifetime.

Slides:



Advertisements
Similar presentations
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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.
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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.
 The position of a method in a program is not critical.  Why?
Road Map Introduction to object oriented programming. Classes
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Week 5 Recap CSE 115 Spring Composition Informally called “has a” Represented in UML with a diamond- headed arc In code: Declare an instance variable.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Week 3 Recap CSE 115 – Fall Java Source Code File Made up of: Package declaration Class definition.
Understanding class definitions Looking inside classes.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
The different kinds of variables in a Java program.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object Oriented Programming (OOP) Lecture No. 11.
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Object Oriented Programming in Java Habib Rostami Lecture 10.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
Nested For Loops. First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
Examples of Classes & Objects
How to Design Supplier Classes
CompSci 230 S Programming Techniques
Object Lifetime and Dynamic Objects
Function There are two types of Function User Defined Function
Using local variable without initialization is an error.
More Object Oriented Programming
Defining Classes and Methods
Overloading and Overriding
Classes & Objects: Examples
Lecture 14- Abstract Classes
Local Variables, Global Variables and Variable Scope
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Welcome back to Software Development!
Submitted By : Veenu Saini Lecturer (IT)
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Classes and Objects Systems Programming.
Classes Member Qualifiers
Scope Rules.
Introduction to Classes and Objects
Presentation transcript:

Summary of the lecture We discussed –variable scope –instance variable declarations –variable lifetime

Variable scope The scope of a variable is the part of a program where a variable declaration is in effect. Variables declared in different ways have different scope.

Local variables A variable declared within a method is a local variable. The scope of a local variable is from the point of the declaration to the end of the method body.

Issue #1 Using a variable in many methods It is often necessary to refer to a variable from many methods in a class. A local variable cannot be used outside of the method in which it is declared.

Instance variables A variable declared as a class member (i.e. within the class body but not within any method) is called an instance variable. The scope of an instance variable is the entire class body. Each instance of a class has its own set of instance variables.

Instance variable declaration An instance variable declaration consists of an access control modifier in addition to a type and a name. A rule in CSE115 is that all instance variables must be declared using the “private” access control modifier

Access Control Modifiers “public” – the member can be accessed from outside the class “private” – the member can be access only from inside the class

Class members: (instance) methods & instance variables Any class member (method or variable declared in the class body, but not inside a method) must have an access control modifier. Our rule: methods are public, instance variables are private. Later in semester we will justify this rule (one we know a little more about the issues involved)

Lifetime In addition to scope, variables have another important property called lifetime. The lifetime of a variable is the time during execution of a program that the variable exists.

Lifetime of a local variable A local variable comes into existence when a method is called, and disappears when the method is completed.

Lifetime of an instance variable Instance variables are created when a class is instantiated. Each object has its own set of instance variables. Instance variables persist as long as their objects persist –as far as we know right now, objects persist until the end of the runtime of the program.