1. What are the lifetime of an instance variable, a local variable, and a parameter?
–The lifetime of an instance variable is the period that the object runs, so equals to the lifetime of the object. The lifetime of a local variable anc a parameter are the same as the life time of the method, they can only be access while the method is run.
2. What is shadowing? Give an example and describe the bad things that shadowing might cause in a program.
–Shadowing is the use of same name in the local variable and the globle variable. For example:
public int x
public void someMethod (int x){
}
— It will be dangerous because the same name may cause a coding error in the program, since the variables sometimes refer to globle and other time to local.
3. Consider the code segment.
a. list the instance variables, parameters, and local variables in this code.
instance: a, b.
local: c, d.
parameter: x, y
b. describe the scope of each variable or parameter:
The scope of a and b are throughout the object, the class SomeClass.
The scope of x, y, c, d are within the method a Mutator.
c. Describe the lifetime of each variable or parameter.
The lifetime of a, b equals to the lifetime of the object, class SomeClass.
The lifetime of x, y, c, d equal to the lifetime of the method Mutator.