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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 539k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer My Spring MVC app is not allowing any logins and… May 17, 2026 at 2:24 am
  • Editorial Team
    Editorial Team added an answer Since the "API" will have to be accessible via normal… May 17, 2026 at 2:24 am
  • Editorial Team
    Editorial Team added an answer MATCHES will let you use regular expressions. May 17, 2026 at 2:24 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

We have a few sections of our application that are using AJAX.NET 5.7.25.1. Our
My application will have some customisation for each company that uses it. Up until
We are attepting to profile our NHibernate application using NHProf. We have a series
In our Swing application, we are using an automated testing tool in QA (Qf-Test)
We recently upgraded our application to .Net 4.0. We were using MbUnit 2.x. With
I'm considering using Spring to assemble the components of my application, but I've got
As part of my Rails application, I've written a little importer that sucks in
Our application has a background thread which spawns a process through System.Diagnostics.Process : Process.Start(
In our application, we are generating some large ASCII log files to an Windows
We are using enterprise library 4.1 and smart client software factory 2008 to our

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.