In this tutorial, we'll explore the static keyword and we'll find out how we can apply the static keyword to variables, methods, blocks and nested classes.
In the Java programming language, the keyword static means that the particular member belongs to a type itself, rather than to an instance of that type.
This means we'll create only one instance of that static member that is shared across all instances of the class.
We can apply the keyword to variables, methods, blocks and nested classes.
In Java, when we declare a variable as static, then a single copy of that variable is created and shared among all instances of that class.
It doesn't matter how many times we initialize a class. There will always be only one copy of static variable belonging to it. The value of this static keyword variable will be shared across all objects of either the same or any different class.
Example:
Let's say we have a Car class with several attributes (instance variables). Whenever we initialize new objects from this Car blueprint, each new object will have its distinct copy of these instance variables.
That's where static variables come in:
public class Car {
private String name;
private String engine;
public static int numberOfCars;
public Car(String name, String engine) {
this.name = name;
this.engine = engine;
numberOfCars++;
}
// getters and setters
}
Key Points to Remember
Similar to static variable, static methods also belong to a class instead of the object. So, we can call them without creating the object of the class.
Example of static Method:
We generally use static methods to perform an operation that is not dependent upon instance creation. In order to share a code across all instances of that class, we write that code in a static method:
public static void setNumberOfCars(int numberOfCars) {
Car.numberOfCars = numberOfCars;
}
We also commonly use static methods to create utility or helper classes so that we can get them without creating a new object of these classes.
Just take a look at Collections or Math utility classes from JDK, StringUtils from Apache or CollectionUtils from Spring framework and notice that all methods are static.
Key Points to Remember
We use a static block to initialize static variables. Although we can initialize static variables directly during declaration, there are situations when we need to do the multiline processing.
In such cases, static blocks come in handy. If static variables require additional, multi-statement logic during initialization, we can use a static block.
The static Block Example
Suppose we want to initialize a list object with some predefined values.
This becomes easy with static blocks:
public class StaticBlockDemo {
public static List<String> ranks = new LinkedList<>();
static {
ranks.add("Lieutenant");
ranks.add("Captain");
ranks.add("Major");
}
static {
ranks.add("Colonel");
ranks.add("General");
}
}
It wouldn't be possible to initialize a List object with all the initial values along with declaration. That's why we've utilized the static block here.
Key Points to Remember
Java programming language allows us to create a class within a class. It provides a compelling way of grouping elements that we'll only use in one place. This helps to keep our code more organized and readable.
The nested class architecture is divided into two:
The main difference between these two is that the inner classes have access to all members of the enclosing class (including private), whereas the static nested classes only have access to static members of the outer class.
In fact, static nested classes behave exactly like any other top-level class but are enclosed in the only class that will access it, to provide better packaging convenience.
Example of static Class
The most widely used approach to create singleton objects is through a static nested class:
public class Singleton {
private Singleton() {}
private static class SingletonHolder {
public static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
We use this method because it doesn't require any synchronization and is easy to learn and implement.
Key Points to Remember
static variable