Java Static Method: Define a Method Independent of an Object

javastackJava was designed almost two decades ago (in 1995) by Sun Microsystems. Like C++, Java is based on an object-oriented model but is much simpler to learn than C++. Java lets you manipulate “objects” which are usually representations of real world objects, like cars, computers, trees, planets, stars and the like.

Java is used today in a very wide range of applications. Most computers, gaming consoles and cell phones support Java in some form. If you want to be a computer programmer, you’re almost certain to be required to at least read Java code. Its simple style makes it the perfect starting point to write your own Android app.

Static Method in Java

In this tutorial, we assume you have beginner-level understanding of Java. We are going to learn how the “static” keyword can be tagged with a method declaration statement for added functionality.

What is a method in Java? A method is a collection of statements, a function, which lets you perform operations on your data. A method has the following syntax:

Access_modifier return_type name_of_method (parameters)
{
//operations to be performed;
}

Here, the “access_modifier”, which includes the static keyword, is the access type that you want to assign to your method. You instruct the computer how to call your method; whether it should be public or private or not, for example. The” return_type” is the data type of value (or the data type of the value of the result) you will receive. The name of your method (“name_of_method”) must always begin with a lowercase letter. “Parameters” are optional. You can pass values to two or more parameters.

Example: Let’s define a method to help you understand the concept better. We will use the keyword “static” so we can continue using this method later:

public static double maximum (double num1, double num2)
{
     double result;
     if (num1 > num2)
      result = num1;
     else
      result = num2;
     return result;
}

Here, the access modifier is “public”- it will be accessible by other objects throughout the body of the program. The “double” refers to the data type that is being returned. The name we gave our method is “maximum” and the parameters we passed are two numbers on which we performed operations in the body of the program. Here is a working program that you can try for yourself:

public class Test {
     /* Initial main method declaration */
     public static void main (String [] args) {
     double a = 4.00001;
     double b = 2.00002;
     double c = maximum (a, b); /**  We send the values of a and b to the maximum method here /
     System.out.println("The max number between " + a +
             " and " + b + " is " + c);
    }
     Public static double maximum (double num1, double num2)
     {
          double result;
          if (num1 > num2)
             result = num1;
          else
             result = num2;
          return result;
     }
}

Output: The result of the program would be: The max number between 4.000001 and 2.000002 is 4.000001.

Explanation: We made a class called “Test” in which we called the method “maximum” we had written earlier. We passed the values of “a” and “b” to the method, where they became “num1” and “num2”. The computer performed operations on them and returned the “result”, which became “c”. We later asked the computer to print the value of “c”, which was the maximum number.

If you noticed, we used the static keyword when we defined the main method:

public static void main (String [] args)

We also used the static keyword when defining the method maximum:

Public static double maximum (double num1, double num2)

Defining Static Methods

The static keyword instructs the computer that the method we are defining is independent of any object and its variables. This means that you can define the method without referencing it to any object- this is why the main method is always static, as we haven’t written any objects yet that it can refer to. Static methods tell the computer that the method is independent in itself. Static methods typically access the data defined within the parameters, perform operations on it and return a result. They don’t need to access object variables in the program. If you attempt to access variables from other objects, the compiler throws up an error. Methods that are not static can access variables from other objects. If you want to define a method as static, you use the following syntax:

Access_modifier static return_type name_of_method (parameters)
{
   //operations to be performed;
}

As you can see, all we did was add the static keyword after the “access_modifier”.

Now, in our example program, we called the static method from within the class “Test”. This was done by the following line of code:

double c = maximum (a, b);

Because our method “maximum” was static, we had to pass the values of “num1” and “num2” when we called it. Also, the method returns a “double” value, which we had to assign to “c” as well. If our method wasn’t static, we would have had to pair it with an object, which would then be passed as the parameter value for the instance method.

If you wanted to access the static method “maximum” in the “Test” class from within the body of another class called “Movie”, you would use the following syntax:

class Movie
{
   //code
   double action = Test.maximum(a, b);
}

Static methods are similar to instance methods. They just receive added functionality and added restrictions. The static method cannot, for example, access non-static data members defined within the body of the class. It also cannot call other non-static methods.

When do You Use Static Methods?

Why do we use static methods? Because static methods are independent, they can be called from different classes and passed different parameter values each and every time. Static methods are also easier to execute, because the computer does not have to search for the value of reference variables (because there aren’t any!).

We hope you have a better understanding of how to use static methods. Remember, the best way to learn programming, is to try it out yourself. Java is a pretty easy language to learn, even for those with no prior programming experience. Once you get a basic feel of it, do try your hand at the more advanced levels, and write your own game or app based on Java.