Questions tagged [static-initialization]

Questions regarding initialization code of static members

292 questions
186
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,659
  • 2
  • 10
  • 17
101
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
  • 107,447
  • 124
  • 346
  • 512
53
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
  • 28,657
  • 41
  • 110
  • 158
46
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,398
  • 3
  • 24
  • 37
40
votes
5 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
  • 51,859
  • 84
  • 263
  • 429
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
  • 15,634
  • 2
  • 33
  • 63
29
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
  • 4,721
  • 7
  • 43
  • 82
25
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,504
  • 3
  • 36
  • 51
22
votes
5 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
  • 123,374
  • 44
  • 277
  • 381
22
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
  • 31,683
  • 83
  • 267
  • 574
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
  • 204,536
  • 44
  • 236
  • 426
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,473
  • 4
  • 21
  • 39
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
  • 867
  • 2
  • 8
  • 10
18
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
  • 17,895
  • 16
  • 55
  • 102
17
votes
1 answer

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
  • 57,395
  • 54
  • 209
  • 347
1
2 3
19 20