Declaring Fields


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 Class

The 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; } 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net