Page 379 - Introduction to Programming with Java: A Problem Solving Approach
P. 379

                 0.0 Last A-Head 345
CHAPTER 9
    Classes with Class Members
Objectives
• Learn how and when to use class variables.
• Learn how to write class methods and when to use them.
• Learn how and when to use class constants.
• Practice some of the design approaches suggested in Chapter 8.
• Optionally, learn how to construct a linked list of objects and access them through class methods.
Outline
          Apago PDF Enhancer
9.1 Introduction
9.2 ClassVariables
9.3 ClassMethods
9.4 NamedConstants
9.5 WritingYourOwnUtilityClass
9.6 UsingClassMembersinConjunctionwithInstanceMembers
9.7 ProblemSolvingwithClassMembersandInstanceMembersinaLinkedListClass(Optional)
9.1 Introduction
When you think about an object-oriented solution, what do you envision? Based on what you’ve learned up to this point, you should see separate objects, each with their own set of data and behaviors (instance vari- ables and instance methods, respectively). That’s a valid picture, but you should be aware that in addition to data and behaviors that are specific to individual objects, you can also have data and behaviors that relate to an entire class. Since they relate to an entire class, such data and behaviors are referred to as class variables and class methods, respectively.
Let’s look at an example. Suppose you are charged with keeping track of YouTube videos. You need to instantiate a YouTube object for each YouTube video, and within each object you need to store attributes like the videographer, the video’s length, and the video file itself. You should store those attributes in instance variables because they are associated with individual YouTube objects. You also need to store attributes like the number of videos and the most popular video. You should store those attributes in class variables because they relate to the collection of YouTube objects as a whole.
   345













































































   377   378   379   380   381