Chapter 5 - Chapter5 Defining&CreatingMultiple Objects...

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

View Full Document Right Arrow Icon
    Chapter 5 Objects
Background image of page 1

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

View Full DocumentRight Arrow Icon
    Instance Variables • Also known as ‘global’ variables, but  they’re not really. • They live at the highest scope level of an  object, so they are available everywhere  WITHIN in the object, but not necessarily  from outside the object.
Background image of page 2
    Instance Variables • They are the “properties” of the object. • They will streamline (avoid repetition in)  your program and make it easier to read.
Background image of page 3

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

View Full DocumentRight Arrow Icon
    Why the Name? • They are called instance variables because  each  instance  of a class (aka each object)  has its own copy of that variable.
Background image of page 4
    Declaring Instance Variables • They are declared inside the class  definition, but outside any methods public class Animal() { private double weight; //… methods go here } • Technically, they can go anywhere, in- between methods or whatever, but why  would you do that?
Background image of page 5

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

View Full DocumentRight Arrow Icon
  Instance Variable Initialization • Instance variables don’t NEED to be  initialized. • In fact, they usually aren’t… why? • Because of this, they are given default  values. public class Animal{
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

Chapter 5 - Chapter5 Defining&CreatingMultiple Objects...

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

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