Chapter9_nn - Chapter 9 Classes with Class Members 1 Class Variables Class Methods How to Access Class Members When to Use Class Members Class

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 9 - Classes with Class Members Class Variables Class Methods How to Access Class Members When to Use Class Members Class Constants Example Program Using Class Members 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Class Variables Based on what you've learned so far, when you're working on an  object-oriented program, you should envision separate objects,  each with their own set of data and behaviors (instance variables  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. For a particular class, each of the class's objects has its own  copy of the class's instance variables. For a particular class, each of the class's objects shares a single  copy of the class's class variables. 2
Background image of page 2
Class Variables If you'd like a variable to be shared throughout its  class, make it a class variable by using the  static   modifier in its declaration: <private or public> static <type> <variable-name> ; Example: public class Mouse { private static int mouseCount; private static double averageLifeSpan; ... Class variables are declared at the top of the class,  above all the methods. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Class variables use the same default values as  instance variables: integer types get  0 floating point types get  0.0 boolean types get  false reference types get  null What are the default values for the class variables in  this code fragment? public class Mouse { private static int mouseCount; private static double averageLifeSpan; private static String researcher; private static int simulationDuration = 730; ... Initializations 
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/19/2012 for the course SOC 1101 taught by Professor A.tan during the Fall '08 term at National University of Singapore.

Page1 / 14

Chapter9_nn - Chapter 9 Classes with Class Members 1 Class Variables Class Methods How to Access Class Members When to Use Class Members Class

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online