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 9017681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:20:00+00:00 2026-06-16T04:20:00+00:00

I have 2 classes: Class A: public class A { static B b =

  • 0

I have 2 classes:

Class A:

public class A {
    static B b = new B();

     static {
         System.out.println("A static block");
     }

     public A() {
         System.out.println("A constructor");
     }
}

Class B:

public class B {
     static {
         System.out.println("B static block");
         new A();
     }

     public B() {
         System.out.println("B constructor");
     }
}

I create a Main class which just creates new A:

public class Main {
    public static void main(String[] args) {
        new A();
    }
}

The output I get is:

B static block
A constructor
B constructor
A static block
A constructor

As you can see, the constructor of A is invoked before its static initializer.

I understand it got something to do with the cyclic dependency I created but I was under the impression the static initializer should always run before the constructor.

What is the reason for this to happen (technically in the java implementation) ?

Is it recommended to avoid static initializers all together ?

  • 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-06-16T04:20:02+00:00Added an answer on June 16, 2026 at 4:20 am
    static B b = new B();
    

    is before

    static {
         System.out.println("A static block");
    }
    

    So you require that the B instance be initialized before you print "A static block".

    And initializing the B class means you need to create a A instance. So there’s no way for “A static block” to be printed before the A instance is constructed.

    Yes, the static initialization of A is launched before the constructor is launched but, apart deadlocking, there would be no other solution to the sequence you require.

    Note the warning in the specification :

    Because the Java programming language is multithreaded, initialization
    of a class or interface requires careful synchronization, since some
    other thread may be trying to initialize the same class or interface
    at the same time. There is also the possibility that initialization of
    a class or interface may be requested recursively as part of the
    initialization of that class or interface; for example, a variable
    initializer in class A might invoke a method of an unrelated class B,
    which might in turn invoke a method of class A.
    The implementation of
    the Java virtual machine is responsible for taking care of
    synchronization and recursive initialization by using the following
    procedure [the doc goes on with the complete procedure]

    A best practice, in Java as in other languages, is basically to avoid cyclic dependencies as their resolution may be very hard to predict.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that I have these three classes: class Foo { void fn() { System.out.println(fn
Let's say we have these two classes: public class Base { public static int
I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{
I have the following classes: public class A { static { B.load(A.class); } public
I have two classes : import android.cla; public class CW { public static void
I have two classes class A { public string something { get; set; }
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
my code is like this: i have two classes first class: public class Box<E>
I have two classes: public class MultilingualString { public int Id { get; set;
I have a 3 classes: Class 1: public IEnumerable<double> Values; public Class2 class2Instance; Class2:

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.