Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should classes be initialised - at load time or at first use?

One can load a class dynamically using this method of java.lang.Class:

public static Class<?> forName(String name, boolean initialize,
                               ClassLoader loader)

According to the JavaDoc, the second parameter is used to control the timing of class initialization (execution of static initialization code). If true, the class is initialized after loading and during the execution of this method; if false, initialization is delayed until the first time the class is used.

Now, I understand all that, but the docs don't say how to decide which strategy to use. Is it better to always do initialization immediately? Is it better to always delay it to first use? Does it depend on the circumstances?

like image 789
Simon Kissane Avatar asked Sep 11 '14 07:09

Simon Kissane


People also ask

Which class is loaded first in Java?

BootStrap ClassLoader: A Bootstrap Classloader is a Machine code which kickstarts the operation when the JVM calls it. It is not a java class. Its job is to load the first pure Java ClassLoader.

What would you use to execute code only once when a class is first loaded?

Unlike C++, Java supports a special block, called a static block (also called static clause) that can be used for static initialization of a class. This code inside the static block is executed only once: the first time the class is loaded into memory.

Why is initialization of variables a must before using it in a Java program?

Local variables must be initialized before use, as they don't have a default value and the compiler won't let us use an uninitialized value.

Do fields need to be initialized before use?

Note: It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.


1 Answers

Yes, it depends on circumstances, but usually it is preferred to just let classes be loaded and initialized on first use.

Cases when you might want to early initialize them (e.g. by calling forName() for them):

  • Static initialization blocks might perform checks for external resources (e.g. files, database connection), and if those fail, you don't even want to continue the execution of your program.
  • Similar to the previous: loading external, native libraries. If those fail (or not suitable for the current platform), you might want to detect that early and not continue with your app.
  • Static initializaiton blocks might perform lengthy operations and you don't want to have delays/lags later on when they are really needed, you can initialize them early or on different, background threads.
  • If you have static configuration files where class names are specified as text, you might want to initialize/load them early to detect configuration errors/typos. Such examples are logger config files, web.xml, spring context etc.
  • Many classes in the standard Java library cache certain data like the HTTPUrlConnection caches the HTTP user agent returned by System.getProperty("http.agent"). When it is first used, its value will be cached and if you change it (with like System.setProperty()), the new value will not be used. You can force such caching if you initialize the proper classes early, protecting them to be modified by the code later on.

Cases when you should not initialize early:

  • Classes which might only need in rare cases, or they might not even be needed at all throughout the run of your application. For example a GUI application might only show the About dialog when the user selects the Help/About menu. Obviously no need to load the relevant classes early (e.g. AboutDialog) because this is a rare case and in most runs the user will not do this / need this.
like image 186
icza Avatar answered Sep 18 '22 05:09

icza