DOC PREVIEW
FIU COP 2210 - Shadowing

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ShadowingComputer Programming I Instructor: Greg ShawCOP 2210 Shadowing(The Shadow knows...how to use this to avoid shadowing!)A common programming error is to use the same name for a methodparameter or local variable as for an instance variable.In that case, the parameter or local variable "shadows" or"hides" the instance variable and the instance variable is notaccessed.For example, suppose we have a class to represent a cylinder (a3-D object shaped like a tin can) with instance variables thatstore the radius and the height of a cylinder object.public class Cylinder{// instance var'sprivate double radius ;private double height ;Here is an example of a constructor that does not access theinstance variables due to shadowing:public Cylinder(double radius, double height){radius = radius ;height = height ;}Instead, this constructor merely sets the parameters to thevalues they already have! In the constructor body, radius andheight are the parameters and not the instance variables, due toshadowing.One way to prevent accidental shadowing is to use the objectreference this with instance variables:public Cylinder(double radius, double height){this.radius = radius ;this.height = height ;}Since this.radius means "the radius instance variable of theobject being constructed" it is not shadowed by the parameter ofthe same name.Now suppose that our Cylinder class has two additional instancevariables that store the volume and surface area of a cylinder:private double volume ; // volume of cylinderprivate double area ; // surface areaAlso suppose that the constructor computes and stores the volumeand area when a Cylinder object is created. Can you spot theerrors in the following constructor? (The errors are not in theformulas).public Cylinder(double radius, double height){this.radius = radius ;this.height = height ;double volume = Math.PI * Math.pow(radius,2) * height ;double area = ( 2 * Math.PI * Math.pow(radius,2) ) + ( 2 * Math.PI * radius * height ) ;}The problem is that the constructor declares local variablescalled volume and area, and initializes them instead of theinstance variables of the same names.Using this eliminates this potential problem because if you tryto use it in a declaration:double this.volume = Math.PI * Math.pow(radius,2) * height ;you will get a syntax error! Although the above example used the class constructor,shadowing may occur in any method Another benefit of using this with instance variables isthat then there is no problem in using the same names formethod parameters, so you do not have to think up so manydifferent variable


View Full Document
Download Shadowing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Shadowing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Shadowing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?