Bits of Java – Episode 14: The static keywork

In this episode we will discuss the meaning of the static keyword and we will see where it can be used in your code.

There are various places in your code when you can use the static keyword. Let's see them.

  • static fields: when you mark a variable as static you are creating a class variable, meaning that the variable is shared among all the instances of the class in which is defined. This is in contrast with instance variables, which are non-static variables defined within a class, and of which every instance of the class gains its own copy.

    Since static variables are shared among all the instances, we actually do not need any instance to call them. We just need the class to be loaded, and that`s it!

    This is probably the most common way to access a static variable. What you may not know (I did not, at least) is that you can also do something like this:

    Here we are setting the instance of MyClass to null, but accessing a static variable through a null instance is still possible and does not throw any runtime Exception. This is because, being a class variable, it exists and it is accessible also if the instance is null as long as the class is loaded!

  • static initializer blocks: static variables can be initialized in the same statement where they are declared, in an instance initializer block or in a static initializer block. If the static variable is also marked as final instead, it can only be initialized in the same statement of the declaration or in a static initializer block, and cannot be initialized more than once.

  • static methods: the static keyword can be also applied to a method, with the exception of abstract methods. This is because abstract methods, by definition, need to be overridden, while, static methods cannot be overridden. Thus, the two keywords, static and abstract are conflicting with each other and cannot be used together.

    That said, a static method, as we anticipated, is a method that cannot be overridden by any subclass of the class in which it is defined, but it can only be hidden. I will not discuss here what these two terms, overriding and hiding, mean, because this will be the topics of the next two weeks, so if you are not familiar with these concepts, be patient, please!

    As for a static variable, a static method can be called without the need of instantiate the class before. This is exactly the reason why the main method is static. The JVM can call it directly by MyClass.main(), so without creating an instance of MyClass.

  • static imports: as the standard imports allow you to import classes written by others and use them in your code, the static import allows you to import static class members into your code and use them. Of course, the standard rules for visibility apply, namely if a static method is marked as private there is no way we can access it from another class.

    Let's say MyClass of the previous example is defined in package mypackage.A and we want to use its static method from another class in another package.

    The naming conventions for static imports are the same as the ones for standard import and the same rules apply for the allowed names (only letters, $, _, . and numbers are allowed; is not possible to start a package name with a number nor a . and a single _ is not allowed).

I hope the discussion on the static keyword was helpful. Next week, as anticipated, we will discuss the concept of method overriding and we will compare that to the one of method overloading!

By Ilenia Salvadori