5.14.1. Classes for Data


Classes with variables

We will see that a class can be viewed in more than one way. For our purposes right now, a class is a type of objects. It tells the variables that objects of this class contain. As a very simple example, consider a class whose objects contain two integers, called a and b. We will mark the variables public to indicate that they can be used by objects of other classes.

  class PairOfInts
  {
     public int a;
     public int b;
  }
If x is an object of type PairOfInts, then x.a and x.b are variables. Think of x as a bundle of two variables.

An object of class PairOfInts is called an instance of class PairOfInts. Variables a and b are called instance variables because they belong to an instance. Static variables belong to the class itself, not to an instance.