150 likes | 164 Views
Reference … and Misc Other Topics. Clark Savage Turner, J.D., Ph.D. csturner@csc.calpoly.edu 756-6133 Some lecture slides have been adapted from those developed by John Lewis and William Loftus to accompany D Java Software Solutions: Foundations of Program Design, Second Edition.
E N D
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. csturner@csc.calpoly.edu 756-6133 Some lecture slides have been adapted from those developed by John Lewis and William Loftus to accompany D Java Software Solutions: Foundations of Program Design, Second Edition
bishop1 References • Recall from Chapter 2 that an object reference holds the memory address of an object • Rather than dealing with arbitrary addresses, we often depict a reference graphically as a “pointer” to an object ChessPiece bishop1 = new ChessPiece();
Before After num1 num1 num2 num2 5 5 12 5 Assignment Revisited • The act of assignment takes a copy of a value and stores it in a variable • For primitive types: num2 = num1;
After Before bishop1 bishop2 bishop1 bishop2 Reference Assignment • For object references, assignment copies the memory location: bishop2 = bishop1;
Aliases • Two or more references that refer to the same object are called aliases of each other • One object (and its data) can be accessed using different variables • Aliases can be useful, but should be managed carefully • Changing the object’s state (its variables) through one reference changes it for all of its aliases
Garbage Collection • When an object no longer has any valid references to it, it can no longer be accessed by the program • It is useless, and therefore called garbage • Java performs automatic garbage collection periodically, returning an object's memory to the system for future use • In other languages, the programmer has the responsibility for performing garbage collection
String Object String Object String Object String Object String Garbage, for example • In Java, Strings cannot be modified. • Whenever you assign a new value to a String, Java must create a new String object and discard the old one. • Consider this example: String myString = “orange”; myString = myString + “ juice”; myString myString + “juice” value orange value orange juice (orphaned object) myString value orange value orange juice
Passing Parameters to Methods • Parameters in a Java method are passed by value • 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) • Passing parameters is essentially an assignment • note that the method’s parameters are local method variables with local scope! • When control returns to the calling method, an actual parameter keeps the value it started with.
Passing Objects to Methods • Objects in a Java method are passed by reference • The actual parameter keeps its address, andthe formal parameter gets a pointer to the actual parameter • Changing the state of a formal parameter inside a methodalso changes the state of the actual parameter • Changing the reference of a formal parameter (so it points to a new object) does not change the pointer of the actual parameter.
Passing Objects to Methods • What you do to a parameter inside a method may or may not have a permanent effect (outside the method) • See ParameterPassing.java (page 226) • See ParameterTester.java (page 228) • See Num.java (page 230) • Note the difference between changing the reference and changing the object that the reference points to
The static Modifier • 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 • For example • 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. • To make a method static, we apply the static modifier to the method definition • The static modifier can be applied to variables as well • It associates a variable or method with the class rather than an object
class Helper public static int triple (int num) { int result; result = num * 3; return result; } Static Methods Because it is static, the method could be invoked as: value = Helper.triple (5);
Static Methods • 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 • Static methods are not part of an instance, so they cannot reference instance variables • Static methods can reference static variables or local variables
Static Variables • Static variables are sometimes called class variables • Class variables are shared among all instances of the class • If a variable is declared as static, only one copy of the variable exists private static float price; • Memory space for a static variable is created as soon as the class in which it is declared is loaded • Instance variables are declared within a class, but not within a method. Each has its own data space in its object. • Local variables are declared within a method; those declared in the parameter list are also local.
Static Variables • All objects created from the class share access to the static variable • Changing the value of a static variable in one object changes it for all others • Static methods and variables often work together • See CountInstances.java (page 233) • See MyClass.java (page 234)