`
`- O L
`
`nJ
`
`MI CROGRAFX—2 04 9
`
`II
`
`- Q 3 O1
`
`;
`
`
`
`; 5 COURSE TECHNOLOGY
`In CENGAGE Learning‘
`
`Object-Oriented Programming
`Using CH. Fourth Edition
`Joyce Farrell
`
`Executive Editor: Marie Lee
`
`Acquisitions Editor: Amy Joltymore
`
`Managing Edhoc Tricia Cole
`
`Developmental Edhoc Lise Rutfolo
`Editorial Assistant: Patrick Frank
`
`Marketing Manager: Bryant chrzan
`
`Content Protect Manager: Erin Dowler
`Art Director: Bruce Bond
`
`Manufeaurtng Coorditator:
`Jullo Esperas
`
`Prootreader: Wendy Benedetto
`
`Cover Designer: Bruce Bond
`
`Cover Photo: 0 iStocirphoto.ooml
`Storman
`
`Compoeion International Typesetting
`and Composition
`
`0 2009 Course Technology. Cengage Leeming
`
`ALL RIGHTS RESERVED. No part ol this work covered by the copyright
`herein may be reproduced. transmitted. stored or used In any form or by
`any means—graphic. electronic. or mechanical. including but not limited
`to photocopying, recording. scanning. digitizing. taping. Web dtsirbutlon.
`lnlormation networks. or inlormatiori storage and retrieval systems. except
`as pennited under Section 107 or 103 of the 1976 United States
`Copyright Act—wlthout the prior written permission of the ptblisher.
`
`For product lntormatlon and technology assistance. contact us at
`Cengege Learning Customer & Sales Support, 1-800-354-9106
`
`Forpermission louse materieltromthtsteudorproduct.
`submit all requests onllne at cengage.cornl‘permlasions
`Further permissions questions can be e-mailed to
`pennlsalonrequestflcengagmcom
`
`ISBN-13: 978-1-4239-0257-7
`
`ISBN-10: 1-4239-0257-2
`
`course Technology
`25 Thomson Place
`
`Boston. MA 02210
`USA
`
`Cengage Learning is a leading provider ol customised learning solutions with
`otfioe locations around the globe. Including Shgapore. the Unled Kingdom.
`Australia. Mexico. Brazl. and Japan. Locate your local otfioe at
`tnternationaI.cengage.comIregion
`
`Cengage Leamlng products are represented in Canada by Nelson
`Education. Ltd.
`
`For your ifelong teeming solutions. visit course.cengage.com
`
`Purchase any oi our products at your local college store or at our
`preterred onite store \vvrur.lcltaptere.com
`
`Some of the product names and company names used it this book have
`been used tor tdentltlcatlon purposes only and may be trademarks or
`registered trademarks of their respective manufacturers and sellers.
`
`Course Technology, a part of Cengage Leeming. reserves the right to
`revise this publication and metro changes irom time to time In its content
`without notice.
`
`Printed in the United States of America
`
`12345671211 100908
`
`Copyright 2009 Cengage Learning. Inc. All Rights Reserved. May not be copied. scanned. or duplicated. in whole or in part.
`
`
`
`CHAPTERSEVEN
`
`USING STATIC CLASS MEMBERS
`
`A CH object is an instantiation of a class that can contain both data members and methods.
`When you create an object. a block of memory is set aside for the data members. Just as the
`declaration int :4; reserves enough space in your system to hold an integer. the declaration
`student onestudent; reserves enough storage in your system to hold a student object. For
`example. if a Student is defined to contain an integer ID number and a double grade point
`average. then each time a student object is declared. enough memory for both the integer and
`the double is reserved. This makes sense. because every student object requires its own ID
`and grade point . verage. Fields such as idnumbez and gradePoint.Average are instance
`fieltb or imtance variables because you store a separate copy of each instance of the class.
`
`When objects are instantiated, each one gets its own block of memory for its data members.
`If you create two students, you reserve two blocks of memory; if you create an array of 100
`objects. 100 blocks of memory are set aside.
`
`Sometimes every instantiation of a class requires the same value. For example, you might
`want every Student object to contain a data member that holds the student athletic lee—a
`value that is the same for all Students. lfyou declare 100 Student objects, all students
`need their own ID and grade point average, but not all students need their own copy of
`the athletic fee figure. If each student: object contains a copy of the athletic fee. you repeat
`the same infonnation l()() times, wasting memory. To avoid this, you can declare the ath-
`letic fee variable as static, meaning that only one memory location is allocated. no matter
`how many objects of the class you instantiate. In other words, all members of the class
`share a single storage location for a static data member of that same class. A class vari-
`able that you declare to be static is the same for all objects that are instantiations of the
`class. Each instantiation of the class appears to be storing its own copy of the same value,
`but each actually just has access to the same memory location. The fields, such as the ID
`number, that are separate memory locations for each instance are non-static.
`
`edtlfl
`otuwvllilahilcremdcvetytiln
`D
`Wlulywenoleonui-Ifllcvlihlowilila
`tholtmion.
`lvclnlraIItltoproIlot.utlno‘nuca
`lIlIcIotI.1lIIvcrldihnlflIhavudlIuouadtI'uaIIltIlluutn
`Wliutwuuuuaunicvlhblmthovuibhtuimiuhsmuiuyuddrusamuulunvinhr
`iuttluptnomn.
`
`‘
`
`DEFINING STATIC DATA MEMBERS
`
`Because it uses only one memory location, a static data member is defined (given a value)
`in a single statement outside the class definition. Most often this statement appears just
`before the class implementation section. Consider the class and program in Figure 7-15. Note
`that in this example, the small student class contains just two fields. In this program. every
`object of the class that you ever instantiate receives its own copy of the non-static idNum
`field, but the same static ath1et:icE‘ee value applies to every class member that is ever
`instantiated. (The first shaded statement in Figure 7-15 shows the declaration of the field.)
`When the ma in (1 function declares two Student objects. at-‘reshman and asophomo re,
`they each possess their own idtlum value. but they share the athletic!-‘ee value. In the
`output shown in Figure 7-16. each object displays the same athletic fee.
`
`301
`
`Copyright 2009 Cengage Learning. Inc. All Rights Reserved. May not be copied. scanned. or duplicated. in whole or in pan.



