Static Methods

A static method is simply a function.
Those who have some maths experience will know that this is an example of a function of x

(1)
\begin{align} y=\sqrt[]{x} \end{align}

In Java we would use the static method Math.sqrt to express the same thing, associating values of the variable x with the variable y.

(2)
\begin{align} double \, y= Math.sqrt(x); \end{align}

Methods provide a way of writing a block of code which can be reused in a program. They also provide a way of breaking a program down into logical sections, making writing, testing and debugging the code easier.

Static methods also provide a way of managing program flow, in the same way that loops and branches do. The methods can be called in order to move form one block of code to the next.

There are many static methods already available in the Java libraries, such as Math.abs(), System.out.println() and Math.sqrt(). We can also write our own.

Static versus instance methods

Static methods are mainly to implement functions while instance methods implement (instantiate) objects or data types. When static methods are called they are called by their class name, while instance methods are called with an object name.

There are some examples of static methods above. An example of an instance method is drawTriangle.

Anatomy of a static method

A typical static method looks like this:

public static double myMethod (double c)
{

//block of code for method
return t;

}

The introductory line is called the signature. It consists of:

  • public static indicates that the method is public and a static method
  • double is the type of the returned value. This can be any data type or void if there is nothing returned.
  • myMethod is the method name.
  • (double c) is the argument variable. Also referred to as the formal parameter.

Properties if static methods

Scope of variables

The scope of variables is limited to the method. Any changes to variables within a static method have no effect on the value of variables in the calling program, with the exception of the return variable.

Argument variables

Argument variables are passed into the method by value, which means that the method takes the value of the variable to use. Any changes to the value of an argument variable within a method has no effect on the calling program. Sop in the example above sqrt() could change the value of c, but this will not change the value of c in the program that called sqrt(). (Unless of course that is what is passed back as t.)

Multiple methods

You can define as many methods as you like in a .java file. They are all independent and can be in any order. The program flow is controlled by the method calls. In Java the purpose of the main method is to start this process.

Calling other static methods

Any static method in a .java file can call any other method in the same file or in an imported library. Methods can also call themselves.

Multiple arguments

Methods can take more than one argument. They just need to be listed with their type in the signature:

public static int addThree (int a, int b, int c)
{

return (a+b+c);

}

Which would be called with:

int total = addThree(2,4,6);

Overloading

Methods which have different argument types are treated as different methods. This means that methods with the same name can be used for simplicity. An example might be if a method is designed to average a list of numbers two version could be written, one with a double arguments and the other with int arguments. The method could be called in the same way and the version which runs is determined by the arguments.

A simple example of this is the Math.abs() method, which has variants for all the primitive data types.

Single return value

A method can only return one value. This can, however, be an array or a complex data type.

Multiple return statements

There can be multiple return statements, however, the method will exit at the first return statement executed.

Side effects

Even a method with a void return can have effects outside the method. For instance it might generate output.

Bibliography
1. full source reference: Introduction to Programming in Java: An interdisciplinary approach , Sedgwick R, Wayne K, Pearson (2006) , pp (188-193)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License