Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
Java Software Solutions
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Chapter 5: Enhancing Classes
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Enhancing classes Visibility modifiers and encapsulation revisited
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
INF 523Q Chapter 5: Enhancing Classes (Examples).
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Static Class Members Wrapper Classes Autoboxing Unboxing.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 4 Objects and Classes Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Chapter 5: Enhancing Classes
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
CSE 1201 Object Oriented Programming Using Classes and Objects.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5: Enhancing Classes
Variables in Java A variable holds either
Chapter 4: Writing Classes
Chapter 5: Enhancing Classes
Creating Objects & String Class
Chapter 4: Writing Classes
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
More Object Oriented Programming
Object Oriented Programming
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Chapter 5: Enhancing Classes
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Java Classes Aliases & Null & This
Object Oriented Programming Review
Packages & Random and Math Classes
Outline Creating Objects The String Class The Random and Math Classes
Presentation transcript:

Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed  by John Lewis and William Loftus to accompany  by John Lewis and William Loftus to accompany D Java Software Solutions: Foundations of Program Design, Second Edition

CSC-101References D Recall from Chapter 2 that an object reference holds the memory address of an object D Rather than dealing with arbitrary addresses, we often depict a reference graphically as a “pointer” to an object ChessPiece bishop1 = new ChessPiece(); bishop1

CSC-101 Assignment Revisited D The act of assignment takes a copy of a value and stores it in a variable D For primitive types: num2 = num1; Before num1 5 num2 12 After num1 5 num2 5

CSC-101 Reference Assignment D For object references, assignment copies the memory location: bishop2 = bishop1; Before bishop1bishop2 After bishop1bishop2

CSC-101Aliases D Two or more references that refer to the same object are called aliases of each other D One object (and its data) can be accessed using different variables D Aliases can be useful, but should be managed carefully D Changing the object’s state (its variables) through one reference changes it for all of its aliases

CSC-101 Garbage Collection D When an object no longer has any valid references to it, it can no longer be accessed by the program D It is useless, and therefore called garbage D Java performs automatic garbage collection periodically, returning an object's memory to the system for future use D In other languages, the programmer has the responsibility for performing garbage collection

CSC-101 String Garbage, for example D In Java, Strings cannot be modified. D Whenever you assign a new value to a String, Java must create a new String object and discard the old one. D Consider this example: String myString = “orange”; myString = myString + “ juice”; String Object value orange myString String Object value orange juice myString + “juice” String Object value orange (orphaned object) String Object value orange juice myString

CSC-101 Passing Parameters to Methods D Parameters in a Java method are passed by value D A copy of the actual parameter (the value passed when method is called) is stored into the formal parameter (that is declared in the method header) D Passing parameters is essentially an assignment note that the method’s parameters are local method variables with local scope! note that the method’s parameters are local method variables with local scope! D When control returns to the calling method, an actual parameter keeps the value it started with.

CSC-101 Passing Objects to Methods D Objects in a Java method are passed by reference D The actual parameter keeps its address, and the formal parameter gets a pointer to the actual parameter D Changing the state of a formal parameter inside a method also changes the state of the actual parameter D Changing the reference of a formal parameter (so it points to a new object) does not change the pointer of the actual parameter.

CSC-101 Passing Objects to Methods D What you do to a parameter inside a method may or may not have a permanent effect (outside the method) D See ParameterPassing.java (page 226) ParameterPassing.java D See ParameterTester.java (page 228) ParameterTester.java D See Num.java (page 230) Num.java D Note the difference between changing the reference and changing the object that the reference points to

CSC-101 The static Modifier D In Chapter 2 we discussed static methods (also called class methods) that can be invoked through the class name rather than through a particular object D For example The methods of Math & Keyboard classes are static The methods of Math & Keyboard classes are static Those of String are not, nor are any others where we instantiate an object first and then invoke methods via the name of the instantiated object. Those of String are not, nor are any others where we instantiate an object first and then invoke methods via the name of the instantiated object.  To make a method static, we apply the static modifier to the method definition  The static modifier can be applied to variables as well D It associates a variable or method with the class rather than an object

CSC-101 Static Methods public static int triple (int num) { int result; result = num * 3; return result; } class Helper Because it is static, the method could be invoked as: value = Helper.triple (5);

CSC-101 Static Methods D The order of the modifiers can be interchanged, but by convention visibility modifiers come first  The main method is static: it is invoked by the system without creating an object D Static methods are not part of an instance, so they cannot reference instance variables D Static methods can reference static variables or local variables

CSC-101 Static Variables D Static variables are sometimes called class variables D Class variables are shared among all instances of the class D If a variable is declared as static, only one copy of the variable exists private static float price; D Memory space for a static variable is created as soon as the class in which it is declared is loaded D Instance variables are declared within a class, but not within a method. Each has its own data space in its object. D Local variables are declared within a method; those declared in the parameter list are also local.

CSC-101 Static Variables D All objects created from the class share access to the static variable D Changing the value of a static variable in one object changes it for all others D Static methods and variables often work together D See CountInstances.java (page 233) CountInstances.java D See MyClass.java (page 234) MyClass.java