Questions regarding initialization code of static members
Questions tagged [static-initialization]
325 questions
                    
                    187
                    
            votes
                
                6 answers
            
        Is final ill-defined?
First, a puzzle:
What does the following code print?
public class RecursiveStatic {
    public static void main(String[] args) {
        System.out.println(scale(5));
    }
    private static final long X = scale(10);
    private static long…
         
    
    
        Little Helper
        
- 1,870
- 3
- 12
- 20
                    123
                    
            votes
                
                13 answers
            
        Java: When is a static initialization block useful?
What's the difference between initialization within a static block:
public class staticTest {
    static String s;
    static int n;
    static double d;
    static {
        s = "I'm static";
        n = 500;
        d = 4000.0001;
    }
   …
         
    
    
        Adam Matan
        
- 128,757
- 147
- 397
- 562
                    59
                    
            votes
                
                6 answers
            
        How to fill a Javascript object literal with many static key/value pairs efficiently?
The typical way of creating a Javascript object is the following:
var map = new Object();
map[myKey1] = myObj1;
map[myKey2] = myObj2;
I need to create such a map where both keys and values are Strings. I have a large but static set of pairs to add…
         
    
    
        Jérôme Verstrynge
        
- 57,710
- 92
- 283
- 453
                    54
                    
            votes
                
                2 answers
            
        Why isn't a qualified static final variable allowed in a static initialization block?
Case 1
class Program {
    static final int var;
    static {
        Program.var = 8;  // Compilation error
    }
    public static void main(String[] args) {
        int i;
        i = Program.var;
        System.out.println(Program.var);
   …
         
    
    
        Ravi
        
- 30,829
- 42
- 119
- 173
                    49
                    
            votes
                
                5 answers
            
        static initialization in interface
When I tried to write something like this:
public interface MyInterface {
    static {
        System.out.println("Hello!");
    }
}
the compiler could not compile it.
But when I wrote something like this:
interface MyInterface {
    Integer iconst…
         
    
    
        Sergey Morozov
        
- 4,528
- 3
- 25
- 39
                    30
                    
            votes
                
                2 answers
            
        How to force gcc to link unreferenced, static C++ objects from a library
I'm using a C++ library that can be built as either a shared or a static library.
This library uses a factory technique, where static objects register themselves when the program starts and the static objects get created.
This works fine as long as…
         
    
    
        Gene Vincent
        
- 5,237
- 9
- 50
- 86
                    30
                    
            votes
                
                4 answers
            
        C++ Nifty Counter idiom; why?
I recently came across the Nifty Counter Idiom. My understanding is that this is used to implement globals in the standard library like cout, cerr, etc. Since the experts have chosen it, I assume that it's a very strong technique.
I'm trying to…
         
    
    
        Nir Friedman
        
- 17,108
- 2
- 44
- 72
                    26
                    
            votes
                
                1 answer
            
        Static initializer runs after the constructor, why?
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 {
    …
         
    
    
        Aviram Segal
        
- 10,962
- 3
- 39
- 52
                    24
                    
            votes
                
                6 answers
            
        How to force a static member to be initialized?
Consider this example code:
template
char register_(){
    return D::get_dummy(); // static function
}
template
struct Foo{
    static char const dummy;
};
template
char const Foo::dummy = register_();
struct Bar
…     
         
    
    
        Xeo
        
- 129,499
- 52
- 291
- 397
                    24
                    
            votes
                
                4 answers
            
        How can I run a static initializer method in C# before the Main() method?
Given a static class with an initializer method:
public static class Foo
{
    // Class members...
    internal static init()
    {
        // Do some initialization...
    }
}
How can I ensure the initializer is run before Main()?
The best I can…
         
    
    
        Matt
        
- 21,026
- 18
- 63
- 115
                    23
                    
            votes
                
                1 answer
            
        Why using parallel streams in static initializer leads to not stable deadlock
CAUTION: it is not a duplicate, please read topic сarefully
https://stackoverflow.com/users/3448419/apangin quote:
The real question is why the code sometimes works when it should not.
  The issue reproduces even without lambdas. This makes me…
         
    
    
        gstackoverflow
        
- 36,709
- 117
- 359
- 710
                    20
                    
            votes
                
                8 answers
            
        Thread-safe initialization of function-local static const objects
This question made me question a practice I had been following for years. 
For thread-safe initialization of function-local static const objects I protect the actual construction of the object, but not the initialization of the function-local…
         
    
    
        sbi
        
- 219,715
- 46
- 258
- 445
                    19
                    
            votes
                
                2 answers
            
        C++ static initialization vs __attribute__((constructor))
Example:
struct Foo { Foo() { printf("foo\n"); } };
static Foo foo;
__attribute__((constructor)) static void _bar() { printf("bar\n"); }
Is it deterministic wether foo or bar is printed first?
(I hope and would expect that constructors of static…
         
    
    
        Albert
        
- 65,406
- 61
- 242
- 386
                    18
                    
            votes
                
                2 answers
            
        Static pthreads mutex initialization
Using pthreads, how would one, in C, initialize a static array of mutexes?
For a single static mutex, it seems I can use PTHREAD_MUTEX_INITIALIZER. But what about an static array of them? As, in for example,
#include <pthread.h>
#define…
         
    
    
        ManRow
        
- 1,563
- 4
- 21
- 40
                    18
                    
            votes
                
                1 answer
            
        Why is this Float constant null when executing the static block?
The following code, when executed, prints nitesh null instead of the expected nitesh 130.
Why isn't n initialized before executing the static block?
class test
{
      static
      {
             System.out.println(test.str+"   "+test.n);
      }
  …
         
    
    
        Nits
        
- 1,067
- 2
- 9
- 10