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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:39:42+00:00 2026-05-16T08:39:42+00:00

I recently read that Java now sports initialisation blocks like the following: class C

  • 0

I recently read that Java now sports initialisation blocks like the following:

class C {

    public C() { /* Instance Construction */ }
    static { /* Static Initialisation */ }
    { /* Instance Initialisation */ }

}

I was particularly interested in the static block. It got me thinking about the static initialisation order problem that affects many a novice C++ user, and typical workarounds for it, such as wrapping the static member in a free function, or using GNU’s __attribute__((init_priority(n))) extension.

I’m looking for some means of writing a method that will be called automatically to initialise the static members of a class, either when the first instance is created or simply at the start of the program, during ordinary static initialisation.

So far this is the best that’s come to mind:

class C {
private:

    class Static {
    public:

        Static();

        int i;
        Foo foo;

    };

public:

    static Static statics;

    // ...

};

C::Static::Static() : i(42), foo("bar") {}

That is, wrap all of the static members in a class and create a static instance of that class, whose constructor serves as a static initialisation function. It’s simple to alter this to instantiate the statics member only when an instance is created, and even to ensure the proper destruction of static members.

The problem with this, of course, is that C::foo becomes C::statics.foo, which is rather unnatural. Is there a way to get around the awkward syntax, or is there a better solution altogether?

  • 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-16T08:39:43+00:00Added an answer on May 16, 2026 at 8:39 am

    Seems not worth the effort to me for the most part. You’re really not improving readability, the unusual construct is going to confuse future programmers maintaining your code, and the addition of the new class increases complexity, thereby exposing you to the potential for more bugs.

    I can see how you rarely might need or want to control the order of static initializations, or for some other reason wrap up all the statics. But from a readability point of view, I prefer the tried-and-true:

    class C
    {
    private:
      static int i;
      static Foo foo;
    };
    
    int C::i = 42;
    Foo C::foo("bar");
    

    If you’ve got a lot of those statics, why not send them all off to their own CPP file? But then again, if you really have that many statics, I wonder if there’s something wrong with the overall design in the first place…

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

Sidebar

Related Questions

I recently read that in newer computers Java's I/O performs better than NIO because
Recently I read that the sign $ is allowed in Java variable names, but
I recently read that getters/setters are evil and I have to say it makes
I've read recently that iOS is message based. Is that similar to an event-based
I recently read in a presentation on Scribd that Facebook had benchmarked a variety
I recently read through Code Complete, and it recommends that I create a project
Recently i read an article is about prevent brute-force attack. It said that automatically
I recently read the the following SO question. What's the best way to store
I recently read the following overflow post: Hidden Features of C# One of the
I recently read that in StringWriter and StringReader are used for writing and reading

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.