Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.

Slides:



Advertisements
Similar presentations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Written by: Dr. JJ Shepherd
Help Session How To Get Started Design Parameters
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)
Summary of the lecture We discussed –variable scope –instance variable declarations –variable lifetime.
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
Road Map Introduction to object oriented programming. Classes
Pointers “Absolute C++” Section 10.1
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Understanding class definitions Looking inside classes.
© 2006 Pearson Education Making Objects 1 of 19 MAKING OBJECTS Views of a Class Defining Your Own Class Declaring Instance Variables Declaring Methods.
Introduction to Methods
The different kinds of variables in a Java program.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
PARAMETERS Making Messages Specific Setting Up Associations Actual and Formal Parameters Return Types Accessor and Mutator Methods.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
© 2006 Pearson EducationInterfaces1 of 28 INTERFACES Declaring Interfaces Implementing Interfaces Using Interfaces Polymorphically Visibility Modifiers.
4.1 Instance Variables, Constructors, and Methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
ECE122 Feb. 22, Any question on Vehicle sample code?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Chapter 5 Introduction to Defining Classes
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Using local variable without initialization is an error.
Functions Inputs Output
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Defining Classes and Methods
Programs and Classes A program is made up from classes
Chapter 6 Objects and Classes
Object based programming in Java
Object based programming in Java
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Review for Midterm 3.
Corresponds with Chapter 5
Presentation transcript:

Local Variables Garbage collection

© 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property from one instance and use it to set a property of another instance? For example, you want to get the current color from one instance of class CSMobile and use it to set the current color of another instance of class CSMobile How can you do this? Answer: use a local variable!

© 2006 Pearson EducationParameters3 of 10 Local Variables We’ve seen instance variables –they operate within the scope of the entire class –an instance variable that belongs to a class can be accessed anywhere in the class — within any method –this also means they cannot be accessed by other classes A local variable is declared within a single method –the local variable’s scope is only that method –this means that the local variable can be used only within the method in which it was created The scope of a variable determines where it can be used –instance variables can be used by all methods of a class –local variables can only be used within the method that creates them –syntactically, scope of any variable is the code between the curly braces of where it is declared Let’s see an example...

© 2006 Pearson EducationParameters4 of 10 Java Code Example /** * Example written to demonstrate the use of * local variables in conjunction with accessor * and mutator methods. */ public class ContrivedExample { private CSMobile _csMobileOne, _csMobileTwo; public ContrivedExample() { _csMobileOne = new CSMobile(); _csMobileTwo = new CSMobile(); _csMobileOne.setColor(java.awt.Color.blue); this.contrivedMethod(); // call the method // _csMobileOne now has a blue color } public void contrivedMethod() { java.awt.Color tempColor = _csMobileOne.getColor(); // accessor // tempColor: blue _csMobileTwo.setColor(tempColor); // mutator //_csMobileTwo now also has a blue color } }

© 2006 Pearson EducationParameters5 of 10 ContrivedMethod Explained java.awt.Color tempColor = _csMobileOne.getColor(); tempColor is a local variable –stores the return value of _csMobileOne.getColor() –no need for modifier (i.e., private ) because tempColor can only be used within ContrivedMethod –once ContrivedMethod has finished execution, the value of tempColor will “go out of scope” — will no longer exist –you will not be able to do anything with that java.awt.Color except in ContrivedMethod –note: by our coding conventions, local variables do not begin with an underscore –You can also use new to create local variables, for example: CSMobile mobile = new CSMobile(); Unnecessary to store certain types of data as instance variables, because only one method needs to use them –local variables are used to store data temporarily –example: storing intermediate data in calculations _csMobileTwo.setColor(tempColor); Pass tempColor as a parameter to setColor –just like you’d pass an instance variable!

© 2006 Pearson EducationParameters6 of 10 Methods and Variable Types Three kinds of variables a method can use: parameters, instance variables, and local variables When do we use which kind of variable? –depends on how we want to deal with the variable outside of the method –different kinds of variables have different levels of accessibility or scope –parameters and local variables exist only within the method –parameters are passed between methods –instance variables exist within the class and are accessible to all methods of the class Class 1 Instance Variables Class 2 Instance Variables Method I Local Variables Method J Local Variables Method M Local Variables Method N Local Variables Parameters/returns

© 2006 Pearson EducationParameters7 of 10 Lost References / Garbage Collection What happens when a variable goes out of scope? –you can no longer use the variable’s name to access the instance it refers to –you lose a reference to that instance How else can you lose a reference to an instance? –assign variable to null (no useful value) –assign variable to some other instance What happens when nothing references an instance? –the instance is garbage collected — it is removed from memory by the Java Virtual Machine So make sure you don’t lose all references to an instance -- else you won’t be able to access it anymore and the instance will disappear!

© 2006 Pearson EducationParameters8 of 10 Garbage Collection Example package PlanetOfTheApes; public class HumanCage { private Ape _guard; public HumanCage() { } public void setEvilGuard() { _guard = new Ape(java.awt.Color.orange); } public void setNiceGuard() { _guard = new Ape(java.awt.Color.black); } }

© 2006 Pearson EducationParameters9 of 10 Garbage Collection When the method setEvilGuard() is called _guard When the method setNiceGuard() is called _guard The old instance of Ape gets erased (goodbye Dr.Zaius!) - works the same for local variables that go out of scope!

© 2006 Pearson EducationParameters10 of 10 Parameters Review Parameter passing checklist –do numbers of actual and formal parameters match? –does type (class) of each actual parameter match class of corresponding formal parameter? –are parameters passed in correct order? –are type and name of parameter in method declaration, but only name (identifier) of parameter present in method call? Parameters are another form of factoring information –class factors out capabilities and properties of instances –parameters create generic methods that deal with different types of data for customization Next, we’ll find out about another form of factoring: