Declaring Methods


Declaring Methods

Classes can also declare methods. Methods are generally used to access and manipulate the fields declared in a class. Methods may also perform such functions as mathematical operations, printing messages, and so on. As with the "Declaring Fields" section, this section will only give a broad overview of methods. More detail about methods is provided in Chapter 9.

There are two general types of methods ”instance and static. Instance methods are associated with an object and called by referencing an object. Instance methods are commonly used to access and manipulate the instance variables of the class. For example, a class representing a gas mixture might define an instance method to compute the mixture enthalpy of the gas.

A static or class method is associated with a class rather than with an instance of a class. You can call a static method without first having to create an instance of the class in which the method is defined. Static methods are usually used for generic operations that are applicable to a wide range of classes. The mathematical square root and power methods in the java.lang.Math class, for example, are implemented as static methods.

Example: Adding Methods to a Class

One problem with the SimpleGas class from the previous example is that because the fields are given private access (which is the preferred access for fields) there is no way to directly access their values outside of the SimpleGas class. To rectify this situation, we will rewrite the class adding instance methods that access the current value of the pressure and temperature variables.

 public class SimpleGas2 {   private double pressure=101325.0;   private double temperature=273.0;   public double getPressure() {     return pressure;   }   public double getTemperature() {     return temperature;   } } 

The getPressure() and getTemperature() methods return the current value of the pressure and temperature variables. Since the methods have public access, they can be called anywhere inside or outside of the SimpleGas2 class. To make use of the SimpleGas2 class, we will write a driver program. The driver will define a main() method that will create a SimpleGas2 object and call the getPressure() and getTemperature() methods on the object.

 public class GasDriver {   public static void main(String args[]) {       SimpleGas2 testGas = new SimpleGas2();     //  The getPressure()and getTemperature() methods     //  are called.     System.out.println("pressure is " +                         testGas.getPressure());     System.out.println("temperature is " +                         testGas.getTemperature());   } } 

When the GasDriver.java code is compiled and run, the output is

 pressure is 101325.0 temperature is 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