Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed."— Presentation transcript:

1 Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. csturner@csc.calpoly.edu756-6133 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

2 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

3 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

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

5 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

6 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

7 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

8 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.

9 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.

10 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

11 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

12 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);

13 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

14 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.

15 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


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

Similar presentations


Ads by Google