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:
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:
- when referring to a static field or method, the fact that it is sometimes possible to omit the class name
(either because the static is defined in the same class, or because we have used a static import) means
that it is not always immediately obvious that a field in question is static; another programmer looking at
the code may accidentally assume that the field or method in question is a 'normal' instance field/method
tied to a particular instance of the object rather than shared across all objects;
- declaring a field static does not make it thread-safe (though static final primitives and or final
references to immutable objects will be);
- static final primitives and strings are treated specially by the Java compiler as actual "constants": if one class contains
a reference to one of these constants, then its value will generally be copied directly (inlined) into the referring
class rather than one class actually referring to the value in the other class;
this can lead to unexpected behaviour if different classes have been compiled at different times
against different "versions" of the constant.
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.