Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : in what order are static final fields initialized?

Okay, so say I have a class that looks like this :

public class SignupServlet extends HttpServlet {     private static final Logger SERVLET_LOGGER=COMPANYLog.open(SignupServlet.class);     private static final ExceptionMessageHandler handler = new ExceptionMessageHandler();        private static final SignupServletObservableAgent signupObservableAgent =          new SignupServletObservableAgent(null, SERVLET_LOGGER); } 

Can I count on the class loader to initialize those fields in order, such that I can rely on SERVLET_LOGGER to be instantiated before signupObservableAgent?

like image 612
sangfroid Avatar asked Dec 15 '10 02:12

sangfroid


People also ask

Are static variables initialized first?

Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn't need any object.

What is the order of static variable initialization across one program?

C++ guarantees that variables in a compilation unit (. cpp file) are initialised in order of declaration. For number of compilation units this rule works for each one separately (I mean static variables outside of classes). But, the order of initialization of variables, is undefined across different compilation units.

What is initialization order in Java?

In Java, the order for initialization statements is as follows: static variables and static initializers in order. instance variables and instance initializers in order. constructors.

How do you initialize a static final variable?

The only way to initialize static final variables other than the declaration statement is Static block. A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.


2 Answers

Yes, they are initialized in the order in which they appear in the source. You can read all of the gory details in The Java Language Specification, §12.4.2. See step 9, which reads:

... execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block, except that final class variables and fields of interfaces whose values are compile-time constants are initialized first ...

like image 104
Laurence Gonsalves Avatar answered Sep 23 '22 16:09

Laurence Gonsalves


I think that initialization of static fields could be re-ordered. At least that is how I understand JMM specification

There are a number of cases in which accesses to program variables (object instance fields, class static fields, and array elements) may appear to execute in a different order than was specified by the program.

like image 45
Petro Semeniuk Avatar answered Sep 23 '22 16:09

Petro Semeniuk