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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:06:17+00:00 2026-05-31T09:06:17+00:00

I think I’ve found an error in some of my code, though it doesn’t

  • 0

I think I’ve found an error in some of my code, though it doesn’t appear to rear its head in all situations. I’m hoping someone smarter than me can definitively say “Yes, this is an error”, and better yet, to suggest an another alternative to my implementation.

I believe the source of the error is how the static fields of two classes are initialized; one (in FooClass) is initialized by referring to the other’s field, and one (in MyUtility) is initialized by creating an object of type Foo. Sorry that sounds off; explanation was never my strong point.

I’ve spent the better part of a day trying to reduce the problem, and have something runnable that appears to demonstrate the problem.

public class Tester {

    static class FooClass {
        static final FooClass ITS_FOO = MyUtility.MY_FOO;
    }

    static class MyUtility {
        static final FooClass MY_FOO = new FooClass();

        static FooClass create() {
            return new FooClass();
        }
    }

    public static void main(String[] args) {
        System.out.println("utility's: " + MyUtility.create()); // Line "A"
        System.out.println("class's:   " + FooClass.ITS_FOO);   // Line "B"
    }
}

I realize this design looks odd, but won’t try to justify it much (the real code is structured “oddly” too, but within separate classes with different visibility etc). I would definitely appreciate suggestions for better ways to do this.

The gist of the problem (at least with this program) is that the FooClass.ITS_FOO field is null when line B executes. If I switch the order of lines A and B, neither of the fields are null.

I’ve seen questions like In what order do static initializer blocks in Java run?, but neither that nor the Java Language Spec appear to describe how this kind of mutually referential initialization is done.

Unfortunately, this sample is so far removed from our real implementation that I’ll probably spend the same amount of time translating any solution back, but that will be worth it to get some explanation.

  • 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-31T09:06:19+00:00Added an answer on May 31, 2026 at 9:06 am

    Yes, if you force FooClass to initialize first, then that will trigger MyUtility initializing. The initializer for MY_FOO will proceed because FooClass is already initializing in this thread, so MY_FOO will be non-null.

    On the other hand, if you observe ITS_FOO from the FooClass constructor (currently just defaulted), you’ll see it’s null there…

    This behaviour is well documented in the spec – the section you’ve linked to gives all the details – but basically it’s a really bad idea to have two types whose static initializers refer to each other. Don’t try to fix it in subtle ways: get rid of the dependency. I realize this may be a pain, but really, it’s not worth thinking about any other fix.

    One way of performing that fix may be to extract a third type with a static initializer which doesn’t depend on any other types, and which both of the other types can depend on.

    Of course, doing less in your static initializers is also helpful 🙂

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

Sidebar

Related Questions

Think of these lines of code : @boss_locations = BossLocation.order('min_level asc').all @discovered = current_user.discovered_locations.includes(:boss_location).all
Think of the following code: static int Main() { byte[] data = File.ReadAllBytes(anyfile); SomeMethod(data);
I think I have found a bug when setting or getting the Me.Top property
I think it will be very hard for you to read the code but
Think that I have many activities,and all I want is this: I have a
Think that says it all?
Think about a speedometer, and imagine all these little strokes around it like 10
I think that there are some issues with the earth Gravity, so I wonder
I think this is a relatively simple problem. I wish to obtain some functionality
I think this should be an easy problem to solve, however its turned into

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.