Java static keyword

In Java, a static field or method is one that belongs to a class as a whole rather than to a specific instance (object) of that class. You can often think of static as indicating a "global" variable or method. Static final variables in Java are often used to implement what are sometimes called global constants in other languages.

Additionally, an inner class can be declared static, meaning that it does not hold a reference to any specific instance of the enclosing class.

Declaring and referring to a static field

A static field intended to be a global constant is declared as follows:

public class Solver {
    private static final int MAX_TRIES = 5;
}

Now, any instance of Solver can refer to MAX_TRIES and it will yield the same value; unlike ordinary non-static fields, different instances of MyClass must share the same value of MAX_TRIES.

Notice that a common (but entirely optional) convention is to use all capitals for the names of static final fields. (This convention probably stems from the similar convention used for values defined with a #define directive in C.)

As with any other field, we could also define the static field to be public, allowing it to be referred to from any class. From outside MyClass, we usually need to fully qualify the static field with the class name (but see the import static option below):

// Referring to 'MAX_TRIES' in code outside MyClass:
int noTries = Solver.MAX_TRIES;

However, it is often better practice to declare the field private and have a public static method that can be used to query the value from outside the defining class.

Non-constant static fields

In some cases, we may not want the static field to be constant. For example, we might want the default 'max tries' value to be overridden. In this case, we can omit the final keyword. We can still declare an initial value:

public class MyClass {
    private static int maxTries = 5;
}

Declaring a static method

Static methods work in a similar way to static fields. We add static to the method signature:

public class GlobalOptions {

    public static int getMaxTries() {
	    return ...
    }
	
}

Now, the static method can be called from elsewhere by specifying the class name:

    int maxTries = GlobalOptions.getMaxTries();

Static imports

Where we are referring to a public static field from one class within another, it is sometimes convenient to omit the class name for the sake of legibility. We can do this with a static import at the top of the class file:

import static MyClass.MAX_TRIES;

public class OtherClass {

    public void myMethod() {
        // No need to specify 'MyClass' because of static import
        int noTries = MAX_TRIES;
    }

}

It is also possible to import all of the constants in a given class as you might expect:

import static MyClass.*;

Common bugs with static fields

Careless use of the Java static keyword can introduce subtle bugs. Here are some potential sources of bugs when using static:


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.