Instance and Class Variables


Instance and Class Variables

In the previous section we discussed the difference between primitive and reference type variables. There are two other fundamental classifications for variables. A class variable is a field declared to be static within a class declaration. An instance variable is a field that is not declared to be static within a class declaration.

Let's first discuss instance variables. An instance variable is associated with an instance of a class rather than with the class itself. Instance is the default type. Every object will have its own copy of an instance variable. Outside of the class in which they are defined, instance variables can only be accessed by a reference to an instance of the class in which they are defined, or by a subclass object that inherits the variable. Instance variables (like all variables) will have an access associated with them that defines how the variable can be accessed. Generally speaking, it is considered bad form to give instance variables public access. More information on variable access is provided later in this chapter.

A class variable is associated with a class rather than with an instance of a class. One copy of the class variable is shared by all instances of the class in which it is defined. The system allocates memory for a class variable when the class in which it is defined is first loaded into the Java runtime environment.

A class variable must be declared as such by including the static keyword in the variable declaration. Class variables can be accessed directly without having to use an object. Class variables are typically given public access because they are generally meant to be accessible outside of the class in which they are defined. A typical use for a class variable is to define a mathematical constant such as p .

Example: Using Instance and Class Variables

A black body is any object that emits a radiative heat flux according to the expression q = s T 4 . The parameter is the emissivity, a material property that characterizes the efficiency of the radiative process and will vary from 0 to 1. The T parameter is the temperature of the body in K. The s parameter is the Stefan-Boltzmann constant and has a value of 5.6697e “12 W /c m 2 K 4 The resulting heat flux will have units of W /c m 2 .

The BlackBody class represents a black body. It defines instance variables that represent the temperature and emissivity. Because we might want to use the Stefan-Boltzmann constant outside of the BlackBody class, we define it as a class variable with public access. We don't want users to have direct access to the temperature and emissivity variables so we assign those variables private access. A complete discussion of variable access modifiers is presented later in this chapter. The BlackBody class also defines the getHeating() method that returns the black body radiative heating.

 public class BlackBody {   public static double SIGMA = 5.6697e-12;   private double temperature, emissivity;   public BlackBody(double emiss, double t) {     emissivity = emiss;     temperature = t;   }   public double getTemperature() {     return temperature;   }   public double getHeating() {     return SIGMA*emissivity*                 Math.pow(temperature,4.0);   }   public static void main(String args[]) {     BlackBody body = new BlackBody(0.85, 1000.0);     System.out.println("T is " +                         body.getTemperature());     double qDot = body.getHeating();     System.out.println("heating rate is " + qDot);   } } 

Output ”

 T is 1000.0 heating rate is 4.81924500000 

Now let's define another class that will access the SIGMA variable from the BlackBody class. Because it is public and static , SIGMA can be accessed outside of the BlackBody class by typing the syntax BlackBody .SIGMA .

 public class GetSigma {   public static void main(String args[]) {     System.out.println("SIGMA is "+BlackBody.SIGMA);   } } 

Output ”

 SIGMA is 5.6697e-12 

You may have noticed there is one problem with the BlackBody class. Since the SIGMA variable has public access the value associated with it can be changed. You obviously don't want to let users change the value of the Stefan-Boltzmann constant. This deficiency can be corrected by declaring the SIGMA variable to be final as will be explained in the "Final Variables" section later in this chapter.



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