Anatomy of a Static Method -images from Sedgewick and Wayne "Programming in Java"
JavaTM Language Keywords Used in sqrt double public return static JavaTM Syntax/Structures Used in Newton method header signature local variable argument scope JavaTM Objects and Methods Used in sqrt Double Math.abs()
Using and defining static methods.
As you know from using Java's Math library, the use of static methods is easy to understand. For example, when you write Math.abs(a-b) in a program, the effect is as if you were to replace that code by the value that is computed by Java's Math.abs() method when presented with the value a-b. This usage is so intuitive that we have hardly needed to comment on it. If you think about what the computer has to do to create this effect, you will realize that it involves changing a program's flow of control. You can define static methods other than main() in a .java file, as illustrated in Newton.java.Using static methods in other programs -from 2.2 Libraries and Clients.
To refer to a static method in one class that is defined in another, we use the same mechanism that you have been using to invoke methods like System.out.printf(): Keep both classes in the same directory in your computer. To call a method, prepend its class name and a period separator. For example, consider Gaussian.java. The definition of one of its methods requires the square root function. For purposes of illustration, suppose that we wish to use the sqrt() implementation from Newton.java. All that we need to do is to keep Gaussian.java in the same directory as Newton.java and prepend the class name when calling sqrt(). If we want to use the standard Java implementation, we call Math.sqrt() if we want to use our own implementation, we call Newton.sqrt(). The files Gaussian.java, Newton.java, and SATmyYear.java implement Java classes that interact with one another: SATmyYear calls a method in Gaussian, which calls a method that calls a method in Newton.
