Variable Scope & Lifetime

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 7: More about Methods 1 Chapter 7 More about Methods.
1 Documenting with Javadoc CS 3331 Fall 2009 How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com.
1 Web Based Programming Section 6 James King 12 August 2003.
CSC 212 – Data Structures Lecture 12: Java Review.
Chapter 2: Everything is an Object ● C++ has many non object oriented features inherited from C. It is a hybrid language meaning that it support different.
The Java Programming Language
Classes CS 21a: Introduction to Computing I First Semester,
Utilities (Part 2) Implementing static features 1.
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
Software Documentation Section 5.5 ALBING’s Section JIA’s Appendix B JIA’s.
Javadoc A very short tutorial. What is it A program that automatically generates documentation of your Java classes in a standard format For each X.java.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Documentation Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
CSI 3125, Preliminaries, page 1 Compiling the Program.
1  lecture slides online
1 Documenting with Javadoc CS 3331 Section and Appendix B of [Jia03] How to Write Doc Comments for the Javadoc TM Tool available from
CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Documentation Javadocs. Design/Documentation An essential ingredient of good Object Oriented programming is known as design by contract. This means that.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Topics Instance variables, set and get methods Encapsulation
1 Documenting with Javadoc How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Defining Your Own Classes II
CS314 – Section 5 Recitation 9
More on Arrays Review of Arrays of ints, doubles, chars
Implementing Subprograms
Object Lifetime and Pointers
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Advanced Programing practices
User-Written Functions
More Sophisticated Behavior
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter No. : 1 Introduction to Java.
Testing and Debugging.
Creating and Modifying Text part 2
Programmazione I a.a. 2017/2018.
Introduction to javadoc
Implementing Subprograms
An Introduction to Java – Part I, language basics
Chapter 1: Computer Systems
Object Oriented Programming
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Applying OO Concepts Using Java
Tonga Institute of Higher Education
Introduction to javadoc
Classes CS 21a: Introduction to Computing I
Defining Classes and Methods
Classes, Objects and Methods
LCC 6310 Computation as an Expressive Medium
Java Looking at our first console application in Eclipse
Corresponds with Chapter 5
Implementing Subprograms
Presentation transcript:

Variable Scope & Lifetime CS 161 February 11, 2000

Overview A few more words about Program #3 JavaDoc Variable Scope

Program #3 Some Changes Some Clarification Some Removals Draw half bar may be special case of drawing full bar Some Clarification Methods are required Documentation for methods are required Some Removals Do not turn in JavaDoc generated files

Software Development Software Engineering Java to the Rescue Documentation Documentation is hard to keep accurate & up to date The source code is the best source of documentation Java to the Rescue Well, a program included with the Java Development Kit (JDK) JavaDoc Reads Java source code and produces documentation

JavaDoc JavaDoc is a command line program which extracts comments from Java source code and formats them into HTML pages Works on public packages, classes, interfaces, methods & variables Also protected methods & variables Extracts comments which begin with /** And Special Keywords

JavaDoc General Tags @since text @deprecated text @see link Where text is a description of the version which introduced this feature @deprecated text Where text is a description of methods which you plan on making obsolete in a future release. You should suggest an alternative @see link Where link is text or an HTML reference tag <a href="otherpage.html">label</a>

Class & Interface Tags @Author Name @Version text Where Name is your (or the program author's) name. You may want to include an email address or a company home page @Version text Where text is a description of this version of the source code

Method Tag @param @param variable description Where variable is one of the formal arguments in the following method The description is a description of what the variable is used for, any restrictions or limitations expected or enforced (e.g. range). It’s not a bad idea to include type information The descriptions provided as examples in the textbook are anemic All of the param tags for a given method need to be grouped together in the same comment without intervening tags.

Method Tag @return @return description @throws class description The description you provide should indicate what the return value is, what it can be used for, special values, units, and so on. @throws class description If your class throws an exception (which must be handled inside of a try-catch block), you would describe it with this tag.

An Example Program Program Fac.java /** A Java Application to demonstrate factorials @author Cay Horstman @version Computing Concepts w/ Java 2 Essentials 2nd Ed @see <a href="http://www.cs.orst.edu/~nsmith/cs161/win00"> For CS 161, Winter 2000</a> */ public class Fac {

Factorial Program, Continued /** The main method is where execution starts. It instantiates a ConsoleReader object to get interactive input from the user. It calls the recursive <B>factorial</B> method. @param args An array of strings consisting of the words on the command line. <I>These are ignored</I> */ public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); System.out.println("Please enter a number:"); int n = console.readInt(); System.out.println(n + "! = ” + factorial(n)); }

Factorial Program, Continued Program Fac.java (continued) /** Recursively computes the factorial of an integer. @param n an integer >= 0 @return n! <I>(if n is out of bounds, a 1 is returned)</> */ public static int factorial(int n) { if (n <= 0) return 1; else int result = n * factorial(n - 1); return result; }

Program Fac.java (continued) /** Computes the factorial of an integer. @param n an integer >= 0 @return n! */ public static int factorial(int n) { if (n == 0) return 1; else int result = n * factorial(n - 1); return result; }

Running JavaDoc Z:\CS161\Factorial\docs>javadoc Factorial.java Loading source file Factorial.java... Constructing Javadoc information... Building tree for all the packages and classes... Building index for all the packages and classes... Generating overview-tree.html... Generating index-all.html... Generating deprecated-list.html... Building index for all classes... Generating allclasses-frame.html... Generating index.html... Generating packages.html... Generating Factorial.html... Generating serialized-form.html... Generating package-list... Generating help-doc.html... Generating stylesheet.css... Z:\CS161\Factorial\docs>

Java Variables Variables in Java can be ether primitives or objects (based on classes) We can divide variables into 4 groups Instance (Class) variables Static Variables Local (method) variables Parameter Variables

Variable Lifetime The lifetime of a variable describes the time A variable is created Through the time that it is destroyed And that storage is available for re-use When an object is constructed (instantiated) All instance variables are created They are initialized according to their declaration statements The position in the Java source code is relatively unimportant It does start at the top and go to the bottom

The Duration of a Variable's Lifetime When the object which created the object is no longer useable The method that created it ends All reference to that object are gone The variable is disposed And the storage space is available for other variables

Static Variables When a class with static variables is referenced for the first time All static variables are created Any initialization is performed When the object which loaded the static class is no longer needed (which is hard to tell) The static variables of that class are destroyed

Parameter Variables Parameter variables are the variables based on the formal arguments to a method These are always copies of the value of primitive values declared elsewhere Or values from expressions These remain 'in scope' throughout the lifetime of that method call They are recreated with each method call

Local Variables These are variables declared within a method Or other code block e.g. for ( int j = 0; … ) { … } It exists only within that code block

Initialization All instance variables are automatically initialized 0 for numbers false for boolean null for objects unless we specify otherwise Parameter variables are initialized with copies of their actual arguments

Key ideas Lifetime (duration) Scope Shadow Alias When the variable (or object) exists in your program Scope Where the variable can be used Shadow When the scope of one variable takes precedence over another of the same name Alias The same object is referenced by two different names

Overview / To Do Program #3 JavaDoc Variables Monday Wednesday Friday Kinds instance, static, parameter & local Lifetime/Duration Scope Shadowing Aliases Monday Testing & Debugging Wednesday OOP & Class Design Program #3 Due Friday Inheritance Quiz #4 WS #4 & HW #5