Running code at application/class startup

It’s not rare the scenario where you end up needing to run some code during the initialization of a given class or of an application. This article will present a few options in how to achieve this task.

Run code when the class is loaded

In this scenario, you want to run some code when a given class is loaded by the JRE, but you want that code to be executed only once, regardless of the number of instances of that class. For this, you can use a static initialization block.

You can also watch the video below, where I show step-by-step the creation of the examples and a few more details:

As we can see, a static initialization block is declared by using the static keyword and then using curly brackets do determine the boundaries of the block. The output of the class below shows that the message is just printed the first time the class is loaded and even before its first instance was created.

Run code every time an instance of a class is created

You may be wondering: “I can do this using the constructor of the class, can’t I?”. Yes, you surely can. But there’s an alternate way that you should be aware of: Initialization block, which has the same idea of the static initialization block, but in this case the code is executed every time an instance is created, right before the constructor. Actually, the code from an initialization block is copied into all the constructors by the Java compiler, so it’s a way to share code without needing to explicitly invoke it from all the constructors.

As you can see, the message is printed every time an instance of the class is created, before the constructor’s code. It’s the same behaviour as adding the code as part of the constructor.

Running code when a JEE application is started

The approaches showed above work very well for when you have code to be run when a given class is initialized/instantiated. However, in many cases you have a web/enterprise application and you want to run code when the application gets deployed. For these cases, you need different ways of running initialization code, and Java EE provides a few ways to achieve that.

Servlet Context Listener

The first approach is to create a Servlet Context Listener and place the logic within the method contextInitialized(). This type of listener is activated as soon as your web application is deployed and then the method contextInitialized() is invoked. See below an example of this approach.

Servlet init method

The Servlet lifecycle specification states that just one instance of a Servlet is created and then the init() method is invoked. However, by default the Servlet is just created the first time it’s invoked, but it’s possible to change this to a eager initialization by using the loadOnStartup attribute, like shown in the code below:

Singleton EJB

If you’re running your application in a JEE container, it’s also possible to create a Singleton EJB and then use the Startup and PostConstruct annotations. The Startup annotation will make sure your EJB is created as soon as the app is deployed and, as result, the method annotated with PostConstruct will be invoked.