What is the purpose of using a static method?

Scott Stanchfield

A static method is simply a method that does not need an instance of a class to execute.

Static methods are most useful as utility methods in classes that don't require state. For example, the java.lang.Math class has several static methods that you can call for calculations. No state is needed, so you don't need an instance of the Math class to perform the methods.

You call static methods using the class name rather than an instance name. For example

   int x = Math.max(x,y);

Static methods are often used to access class variables. Class variables are variables in the class that are declared static. There is only one instance of the class variables on the system, so they make a great place to store constant values, "global" counts, and singleton instances.

Often, developers will define variables like

  private static int count = 0;

and define a methods to access it, like

  public static int getCount() {
    return count;
  public static void incrementCount() {

This allows reading and modification of the class variables while retaining control of the values. Noone can directly access private class variables other than this class, so you can restrict the types of updates (for example, only incrementing the count).

Note that static methods cannot access any instance variables defined in the object. Why? Because you don't have an instance of the object to know which instance variables you want to access.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact