
Class vs. Object
Without really defining the difference, you have now seen and worked with both classes and objects. The
difference is very important.
• Class: A template for object creation or a user-defined type.
• Object: An instantiation of a class, something concrete created using a class template.
There are several very good ways to think about this, and these analogies can be powerful mnemonics.
The Blueprint/House analogy for the Class/Object relationship is probably the simplest way to understand
the difference between a class and an object. Blueprints tell you a lot about a house, but you can’t live in a
blueprint. It provides detailed instructions about what a house is supposed to look like. A house is (or in
some cases many houses are) created from the blueprint.
Java allows you to tie variables not only to each object but also to the class. To demonstrate using the
Blueprint/House analogy, you can read a blueprint to get much of the information about any house. You can
tell how many bedrooms there will be just by looking at the blueprint. However, you can’t tell what color
each of those rooms will be until you actually build the house. You can see that there is a sink, but you can’t
use the sink until the house is built.
Data associated with the class instead of the object is stored in class variables (versus instance variables for
objects or instances). Class variables are also called static variables. They are called static variables because
the static keyword is used to identify them.
Class vs. Object
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Class
|
Object
|
Blueprint
|
House
|
Cookie Cutter
|
Cookie
|
|
Services