A class can declare any number of fields that are used to store data for a class. Fields can be of primitive or reference type, they can be instance or static, and so on. A static field is also called a class variable. A nonstatic field is also known as an instance variable. Variables are discussed in much more detail in Chapter 8, but a brief example is provided here. Example: Defining a Simple ClassThe SimpleGas class encapsulates in a very simplistic way a gas mixture. The thermodynamic state of a gas mixture can be characterized by specifying two state variables. In this case, we are specifying the pressure and temperature. To represent this, the SimpleGas class declares two primitive-type instance variables named pressure and temperature . The variables are given private access and are initialized when they are declared. For more information about declaring variables and about variables in general, consult Chapter 8. public class SimpleGas { private double pressure = 101325.0; private double temperature = 273.0; } |