The Lifetime of a Variable

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
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.
 The position of a method in a program is not critical.  Why?
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Access to Names Namespaces, Scopes, Access privileges.
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 02 / 12 / 2007 Instructor: Michael Eckmann.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
The different kinds of variables in a Java program.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
COMP More About Classes Yi Hong May 22, 2015.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Methods Review. 2. Class-wide vs. local variables. 3. Why C# bans global variables. 4. Nested blocks. 5. Scope of identifiers.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
The if StatementtMyn1 The if Statement The basic if statement allows your program to execute a single statement, or a block of statements enclosed between.
August 6, 2009 Data Types, Variables, and Arrays.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it.
Topics Instance variables, set and get methods Encapsulation
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
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.
Advanced Programming in C
Computer Programming Your First Java Program: HelloWorld.java.
Inner Classes.
Inner Classes 27-Dec-17.
Name Spaces: ALL versus OOL
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
C Functions -Continue…-.
Object Oriented Programming
Storage class in C Topics Automatic variables External variables
CompSci 230 Software Construction
Namespaces, Scopes, Access privileges
Lecture 4-7 Classes and Objects
Decision statements. - They can use logic to arrive at desired results
Scope, Visibility, and Lifetime
Variables ICS2O.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Java Language Basics.
Compound Statements A Quick Overview
class PrintOnetoTen { public static void main(String args[]) {
Namespaces, Scopes, Access privileges
Scope of variables class scopeofvars {
Method of Classes Chapter 7, page 155 Lecture /4/6.
Classes, Objects and Methods
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Introduction to Object-Oriented Concepts in Java
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSE 341 Lecture 11 b closures; scoping rules
STORAGE CLASS.
A Methodical Approach to Methods
Loops CGS3416 Spring 2019 Lecture 7.
Methods Scope How are names handled?
Scope Rules.
Inner Classes 25-Oct-19.
Presentation transcript:

The Lifetime of a Variable So far, all of the variables that we have been using were declared at the start of the main() method. However, Java allows variables to be declared within any block. A block defines a scope. Thus, each time you start a new block, you are creating a new scope. A scope determines what objects are visible to other parts of your program. It also determines the lifetime of those objects. The Lifetime of a Variable tMyn

Most other computer languages define two general categories of scopes: global and local. Although supported by Java, these are not the best ways to categorize Java’s scopes. The most important scopes in Java are those defined by a class and those defined by a method. A discussion of class scope (and variables declared within it) is deferred until later, when classes are described. The scope defined by a method begins with its opening curly brace. However, if that method has parameters, they too are included within the method’s scope. The Lifetime of a Variable tMyn

Indeed, the scope rules provide the foundation for encapsulation. As a general rule, variables declared inside a scope are not visible (that is, accessible) to code that is defined outside that scope. Thus, when you declare a variable within a scope, you are localizing that variable and protecting it from unauthorized access and/or modification. Indeed, the scope rules provide the foundation for encapsulation. Scopes can be nested. For example, each time you create a block of code, you are creating a new, nested scope. When this occurs, the outer scope encloses the inner scope. The Lifetime of a Variable tMyn

This means that objects declared in the outer scope will be visible to code within the inner scope. However, the reverse is not true. Objects declared within the inner scope will not be visible outside it: The Lifetime of a Variable tMyn

public static void main(String[] args) int x=10; if(x==10) int y=11; package scope; public class Main { public static void main(String[] args) int x=10; if(x==10) int y=11; System.out.println("Both variables are known here: "+x+" and "+y); } y=y+1; System.out.println("The variable y is not known here!"); The Lifetime of a Variable tMyn

Both variables are known here: 10 and 11 run: Both variables are known here: 10 and 11 Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: variable y location: class scope.Main at scope.Main.main(Main.java:13) Java Result: 1 BUILD SUCCESSFUL (total time: 21 seconds) The Lifetime of a Variable tMyn

Variables are created when their scope is entered, and destroyed when their scope is left. This means that a variable will not hold its value once it has gone out of scope. Therefore, variables declared within a method will not hold their values between calls to that method. Also, a variable declared within a block will lose its value when the block is left. Thus, the lifetime of a variable is confined to its scope. If a variable declaration includes an initializer, that variable will be reinitialized each time the block in which it is declared is entered: The Lifetime of a Variable tMyn

public static void main(String[] args) for(int x=0; x<3; x++) package lifetime; public class Main { public static void main(String[] args) for(int x=0; x<3; x++) int y=-1; System.out.println("The variable y is always "+y); y=y+1; System.out.println("Later it is always "+y); } The Lifetime of a Variable tMyn

The variable y is always -1 Later it is always 0 run: The variable y is always -1 Later it is always 0 BUILD SUCCESSFUL (total time: 0 seconds) The Lifetime of a Variable tMyn

Although blocks can be nested, no variable declared within an inner scope can have the same name as a variable declared by an enclosing scope. This differs from C/C++ environment. The Lifetime of a Variable tMyn