Many people are going crazy whenever they see the static
keywoard inside some source code. That’s ridiculous, because it alleges the people who developed Java were totally stupid.
It is true that the static
keyword gets abused by many people, but there’s a difference between static abuse and proper use of the keywoard.
What does static
mean?
To explain, I have to assume that you already know basic Java concepts.
As you know, Objects can have different fields and methods. For example, look at this class:
public class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } }
This class can be used to create as many persons as you want. Every person has their own name
and age
, which is why those fields are NOT declared as static
. Both name
and age
are so-called instance variables, meaning they can be changed per object instance.
Utility classes
However, sometimes you write classes that do not depend on their own instances. A good example for that are Utility classes, that do not need any instance variables.
Let’s assume you frequently need to convert Location objects into a nice formatted String, something like this:
X: 125, Y: 64, Z: -455 (world_nether)
You can easily achieve this by writing a class that looks like this:
public class Utils { public String locationToString(Location location) { int x = location.getBlockX(); int y = location.getBlockY(); int z = location.getBlockZ(); String world = location.getWorld().getName(); return String.format("X: %d, Y: %d, Z: %d (%s)",x,y,z,world) } }
This method is working totally fine, but it requires an instance of the Utils class. However, this method does not use any instance variables at all, which means we can simply declare it as static without problems.
If we do not declare the method as static, we would have to use this code to call it:
new Utils().locationToString(myLocation);
If we would simply add the static
keyword to the locationToString method, no instance creation is needed to call it, which means that the JVM doesn’t have to create a new object everytime, allocate RAM for it and also doesn’t have to check whether references to this object exist everytime the Garbage Collector runs:
Utils.locationToString(myLocation);
Static fields
You can also declare some fields as static. You can do so when the field describes something about the class itself, that’s not related to the instance. For example, lets imagine you made a Person class and you assume that no person can ever reach an age above 150 years:
public class Person { private final static int MAX_AGE = 150; private final String name; private int age; public Person(String name, int age) { this.name = name; if (age <= MAX_AGE) { this.age = age; } else { throw new IllegalArgumentException("No one can be older than 150 years!"); } } public void setAge(int age) { if (age <= MAX_AGE) { this.age = age; } else { throw new IllegalArgumentException("No one can be older than 150 years!"); } } }
Singleton classes
Sometimes you have a class that will never have to be instantiated more than once. For example, let’s assume you have some PlayerManager class. You could of course instantiate it in your main class and retrieve it whenever you need it, but – why? Since you KNOW that you never need more than one instance of it, you can easily declare all its fields and methods to be static
.
TL;DR
You can declare everything as static
that either describes stuff about the class itself, or that will never be changed in any object instances.
Join my Discord Server for feedback or support. Just check out the channel #programming-help
🙂