3 2 Scope 3 3 accessed Intro to Programming II Scope and Parameters The garbage collector reclaims a local variable when the method ends Java has three types of scope Local scope the variable exists only within a method Object scope the variable can be accessed from any Local variables are useful for temporary variables and counters raise x to the yth power public int exponentiate int x int y int total int i for i 0 i y i total total x return total method belonging to an object Class scope the variable can be accessed by all instances of a class Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p 1 3 4 Object scope Department of Computer Science University of San Francisco p 2 3 5 Class scope Department of Computer Science University of San Fra 3 6 Variables are available anywhere within an object Class variables are available to all members of a class This is useful for data associated with an object that will be These are declared as static used by multiple methods This is also called instance data This means that one copy of the variable is shared by all Class scope public class circle public static final double pi 3 14 public int radius public double getArea return pi radius radius objects public class circle public int radius public static final double pi 3 14 Local scope Exists only when a method is executing Scope refers to the area of a program where a variable can be Useful for defining constants public static void main String args circle c1 new circle circle c2 new circle c1 radius 5 c2 radius 6 System out println c1 s area is c1 getArea System out println c2 s area is c2 getArea public double getArea return pi radius radius Department of Computer Science University of San Francisco p 4 Department of Computer Science University of San Francisco p 5 Department of Computer Science University of San Fra 3 7 Scope 3 8 Parameters 3 9 In the previous example each circle had it s own copy of radius Parameters are the variables passed into a method They all shared a copy of pi We can talk about Formal parameters these are the variables named in the radius has object scope whereas pi has class scope Identify variables in Bank account program method definition Actual parameters these are the variables in the method invocation Department of Computer Science University of San Francisco p 7 3 10 Method signature Type of return variable A method signature allows the compiler to uniquely identify a method bankacct b new bankacct paycheck 100 0 this is a method invocation b depositFunds paycheck Department of Computer Science University of San Francisco p 8 3 11 Specification of all data types coming in and out of a method Type and order of input parameters Example This is a method definition public double depositFunds double amt balance balance amt return balance Example Department of Computer Science University of San Fra 3 12 Consider the following method declaration double calculate double a1 double a2 double a3 Which of the following are valid calls to this method calculate 3 52 0 5 1 Questions What happens in this case public class circle public int radius 5 public void printArea int radius 4 System out println Area is radius radius 3 14 double y calculate 0 1 1 2 2 calculate 1 1 2 3 calculate Hello 4 4 2 calculate calculate 3 3 Department of Computer Science University of San Francisco p 10 Department of Computer Science University of San Francisco p 11 Department of Computer Science University of San Fran 3 13 Questions 3 14 What happens in this case Questions 3 15 public class bankacct public double balance public void updateBalance double newAmount double newAmount 12 0 balance newAmount Specifying scope In general it s a bad idea to give local variables the same name What happens in this case as an instance or class variable Confusing leads to bugs public class circle public int radius 5 public void printArea int radius System out println Area is radius radius 3 14 circle c new circle c getArea 3 If it can t be avoided you can use this to indicate the instance variable should be used rather than the local varaible You can use the class name e g circle pi to indicate that a class variable should be used Department of Computer Science University of San Francisco p 13 Department of Computer Science University of San Francisco p 14 Department of Computer Science University of San Fran
View Full Document
Unlocking...