Static Methods
A static method in Java is a method that belongs to the class itself, rather than an instance of the class. It can be called directly on the class without creating an object of that class. They can be accessed using the class name followed by the method name, like ClassName.methodName().
There are some static methods already included in Java for use. These include but are not limited to…
Double.parseDouble()Integer.toString()Math.sqrt()Math.abs()Math.pow()
In addition to these “included” static methods, Java allows users to create their own methods. Creating a method is done with a header as follows:
public static int doubleMe( double x )
{
}public: Modifier Keyword
static: Modifier Keyword (Tells Java that the method is a static method)
int: Method Return Type
doubleMe: Name of New Static Method
double x: Formal Parameter that can be used within the method. More parameters can be included by using commas (,) within the same parenthesis.
Within the braces after the method header, code is added for the method body. The method body is the code that gives instructions for what the method is supposed to do. At the end of the method body, a return statement is included (unless the statement is of return type void) with the code immediately after being returned to the place where the method was called.
The signature of a method is the method’s name and its parameters.
Overloaded Methods
A method name can be reused if the signature of that method is different. A method with more than one signature is called an overloaded method. All versions of an overloaded method are not required to have the same return type, though they often do.