Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property."— Presentation transcript:

1 Local Variables Garbage collection

2 © 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!

3 © 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...

4 © 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 } }

5 © 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!

6 © 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

7 © 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!

8 © 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); } }

9 © 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!

10 © 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:


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

Similar presentations


Ads by Google