Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 906207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:21:00+00:00 2026-05-15T16:21:00+00:00

Our application is using initialization code that depends on the order static code is

  • 0

Our application is using initialization code that depends on the order static code is executed and I’m wondering if this order will be consistent across all JVMs.

Here is a sample of what I mean:


public class Main {

    static String staticVar = "init_value";

    public static void main(String[] args) {

        System.out.println(A.staticVar);
        staticVar = "mainValue";
        System.out.println(A.staticVar);
    }
}

public class A {
    static String staticVar = Main.staticVar;
}

will give:

init_value
init_value

and


public class Main {

    static String staticVar = "init_value";

    public static void main(String[] args) {

        // System.out.println(A.staticVar);
        staticVar = "mainValue";
        System.out.println(A.staticVar);
    }
}

public class A {
    static String staticVar = Main.staticVar;
}

will give (on my environment):

mainValue

To summarize, across all JVMs, is static code always executed when we use a class for the first time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:21:01+00:00Added an answer on May 15, 2026 at 4:21 pm

    EDIT: Despite all the reassurances below, if you’re thinking of relying on this sort of thing, I would try hard to refactor your code so that it doesn’t crop up. While it is guaranteed to work, it’s also likely to make your code very brittle. The fact that static initializers get called “invisibly” makes them relatively hard to reason about and debug.


    Yes, this is guaranteed by the language specification. From section 8.7 of the spec:

    Any static initializers declared in a class are executed when the class is initialized and, together with any field initializers (§8.3.2) for class variables, may be used to initialize the class variables of the class (§12.4).

    StaticInitializer: static Block

    It is a compile-time error for a static initializer to be able to complete abruptly (§14.1, §15.6) with a checked exception (§11.2). It is a compile-time error if a static initializer cannot complete normally (§14.21).

    The static initializers and class variable initializers are executed in textual order.

    And from section 12.4:

    Initialization of a class consists of
    executing its static initializers and
    the initializers for static fields
    declared in the class. Initialization
    of an interface consists of executing
    the initializers for fields declared
    in the interface.

    Before a class is
    initialized, its direct superclass
    must be initialized, but interfaces
    implemented by the class need not be
    initialized. Similarly, the
    superinterfaces of an interface need
    not be initialized before the
    interface is initialized.

    A class or interface type T will be
    initialized immediately before the
    first occurrence of any one of the
    following:

    • T is a class and an instance of T is
      created.
    • T is a class and a static
      method declared by T is invoked.
    • A
      static field declared by T is
      assigned.
    • A static field declared by T
      is used and the field is not a
      constant variable (§4.12.4).
    • T is a
      top-level class, and an assert
      statement (§14.10) lexically nested
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So we have our J2EE application using Log4j like this public class CustomerController {
We have this huge application that has 18 projects in our source control (
We have written Web application using ASP.NET MVC, which will be hosted in our
I am building our Web-application using Maven Assembly plug-in. This is motivated by a
I'm not a fan of using ASP.NET session state, but our application is using
Our application is developed using Spring framework. Is it good practice to check null
In our application we are using set of images as slides. If we click
Goal : Our application is built using multiple types (e.g. Person, PersonSite(ICollection), Site -
I am developing a PHP application using our XAMPP setup as a test server.
After a couple of months of using our application (which internally uses Jackrabbit 1.6.4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.