Flylib.com

Books Software

 
 
 

Static Methods


Static Methods

The second general category of methods is static, or class, methods. A static method is associated with a class rather than with an individual object. A static method is designated as such by including the static keyword in the method declaration. Static methods are often used to implement generic functionality that can be used by a wide variety of classes. As such, static methods are often given public access.

A static method can be called inside the class in which it is defined simply by typing the method name followed by any input arguments. To call a static method outside of the class in which it is defined, you must type the class name, a period, the method name , and any input arguments.

class_name.method_name(arguments);

You can also call a static method through an object reference, but this is never necessary and is considered a bad practice.

Static methods can only access other static members . You must use an object to call an instance method from within a static method. Static methods can only act upon static variables. A static method will not have access to any instance variables since these are associated with an object rather than with the class itself. Static methods cannot reference the this or super keywords since these refer to objects as well.

Example: Defining Static Methods

Static methods are often used for implementing mathematical functions that can be used in a variety of applications. The StaticDemo class defines a static method named hypotenuse () that returns the hypotenuse of a right triangle. The method is given public access so it can be called directly outside of the StaticDemo class. Note that the hypotenuse() method itself calls a static method, sqrt() from the Math class.

public class StaticDemo
{
  public static double hypotenuse(double height,
                                  double width) {
    return Math.sqrt( height*height + width*width );
  }
}

To access the hypotenuse method outside of the StaticDemo class, you must type StaticDemo.hypotenuse along with the method arguments. The CallStatic class demonstrates how this can be done.

public class CallStatic {
  public static void main(String args[]) {

    //  Call the hypotenuse method

    System.out.println("hypotenuse is " +
            StaticDemo.hypotenuse(3.0, 4.0));
  }
}

Output ”

hypotenuse is 5.0

The main() Method

The main() method is a special static method that serves as the entry point for Java applications. It is declared as static because the main() method is called by the Java runtime before any objects are created. The main() method can call static methods , declare local variables , create objects, and manipulate them. The general syntax of the main() method is ”

public static void main(String args[]) {
   //  body of method
}

An alternative, and perfectly acceptable, syntax for the main() method is ”

public static void main(String[] args) {
   //  body of method
}

The main() method must be declared in the class whose name is invoked to start the program. If you wanted to run a program using the syntax java BlackBody , the BlackBody class would have to declare a main() method.

The main() method can take command line arguments. These are included in the syntax to run the Java program and are passed to the main() method as a String array. For example, the following syntax would run the program MyProgram.class with two command line arguments named arg1 and arg2

java MyProgram arg1 arg2

The command line argument arg1 would be placed into args[0]. The command line argument arg2 would be placed into args[1] . Command line arguments are always passed to main() as String objects. If a command line argument is intended to be a number, you can convert it from a String to the desired primitive type. Command line argument delimiters are white space. If an argument has white space in it, surround it with double quotes. For example, the text "Bobby McGee" (with the double quotes) would be treated as a single command line argument.

Example: Using Command Line Arguments in main()

The main() method of the CommandLine class calls the static hypotenuse () method described in the "Defining Static Methods" example but the inputs for the method are obtained from command line arguments. The command line arguments are passed to main() as String objects. The String objects must be converted to type double before being passed to the hypotenuse() method. This is done using the static method parseDouble() from the Double class.

public class CommandLine {
  public static void main(String args[]) {

    //  Convert the command line arguments from
    //  Strings to doubles.

    double height = Double.parseDouble(args[0]);
    double width = Double.parseDouble(args[1]);

    //  Call the hypotenuse method

    System.out.println("hypotenuse is "+
            StaticDemo.hypotenuse(height, width));
  }
}

Output ”

Your output will vary according to the command line arguments you provide. If you run this program by typing

java CommandLine 3.0 4.0

you will get the output

hypotenuse is 5.0.