Accessing Variable Values


The way that you access the value of a variable depends on whether you are inside or outside the class in which the variable is defined and whether the variable is instance or static. The value of a variable can always be accessed directly inside the class where it is defined by using the simple name of the variable. The value of a variable can be directly accessed outside the class in which the variable is defined only if the variable is given public (and, in some cases, protected ) access.

If it has the proper access, the value of an instance variable can be accessed outside of its class through an instance of the class. The syntax to access the value of the variable is the reference to the object, a period, and the variable name. For example, if you had a reference variable named body that was accessing a public variable named pressure , you would type the syntax

 body.pressure 

The syntax for accessing the value of a class variable outside of the class in which the variable is defined is the name of the class in which the variable is defined, a period, and the name of the variable. For instance, to access the static PI variable defined in the java.lang.Math class, you would type

 Math.PI 

As we said before, you generally should not give instance variables public access. The preferred way of doing things is to give instance variables private access and provide methods to access and/or change their value.

Example: Accessing Variables

Let us modify the BlackBody class from the "Using Instance and Class Variables" example earlier in this chapter, so the class doesn't define a main() method. We will make use of the BlackBody2 class through a driver class. The BlackBody2 class still defines one static and two instance variables. The instance variables are given private access, meaning they are available inside the BlackBody2 class but inaccessible outside of the class.

 public class BlackBody2 {   public static double SIGMA = 5.6697e-12;   private double temperature, emissivity;   public BlackBody2(double emiss, double t) {     emissivity = emiss;     temperature = t;   }   public double getTemperature() {     return temperature;   }   public double getHeating() {     return SIGMA*emissivity*Math.pow(temperature,4.0);   } } 

The BlackBody2Driver class simply creates a BlackBody2 object and tries to access its data members . The temperature variable has private access, so an attempt to directly access the variable with the syntax body.temperature will fail to compile. To access the temperature variable, you must call the getTemperature() method on the BlackBody2 object.

Inside the BlackBody2 class, the class variable SIGMA can be accessed using only its simple name. Since the variable SIGMA has public access it can be freely accessed outside of the BlackBody2 class. To access the SIGMA variable outside of the BlackBody2 class, you have to use its fully qualified name, BlackBody2.SIGMA .

 public class BlackBody2Driver {   public static void main(String args[]) {     BlackBody2 body = new BlackBody2(0.85, 1000.0);   //   This won't work   //     System.out.println("T is "+body.temperature);     //  This is the proper way to access the temperature     System.out.println("T is " +                         body.getTemperature());     //  You can access the class variable directly     System.out.println("Stefan-Boltzmann constant is " +                           BlackBody2.SIGMA);   } } 

Output ”

 T is 1000.0 Stefan-Boltzmann constant is 5.6697e-12 


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