Questions tagged [static-initialization]

Questions regarding initialization code of static members

325 questions
1
vote
1 answer

How to debug static initialization problems?

I'm having a hard time debugging this issue: https://github.com/cnjinhao/nana/issues/445 I have previously asked about LTO vs static object initialization and edited the library's code to implement Meyer's singletons so that the correct order of…
Xeverous
  • 973
  • 1
  • 12
  • 25
1
vote
0 answers

Detailed Initialization Procedure class variable initializer

I saw many confusing answers about the following point : Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single…
1
vote
2 answers

Constant initialization of dependent non-local constant float variables w/ static storage duration in different translation units

I'm wondering whether I can rely on constant initialization when there is a dependency between two constant non-local float variables with static storage duration in two different translation units - where one is dependent on (initialized to [the…
dfrib
  • 70,367
  • 12
  • 127
  • 192
1
vote
3 answers

Is it bad practice to specify an array size using a variable instead of `#define` in C++? (C error: variably modified at file scope)

In C, declaring an array size using a variable, even if it is a const variable, is not allowed. Example: this fails to compile in C: #include const int SIZE = 2; int a[SIZE]; int main() { a[0] = 1; a[1] = 2; printf("%i, %i",…
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
1
vote
2 answers

Plain "C" static initializer macro with variable length tail.

I have a struct defined as: typedef struct coro_context { int id; jmp_buf env; list_head list; jmp_buf waiter; long timeout; void *private; char stack[0]; } coro_context; which I need to initialize with three values id…
ZioByte
  • 2,690
  • 1
  • 32
  • 68
1
vote
0 answers

Why can't interfaces have static initialization block when it can have static methods alone?

After java 8 it is known that interfaces can have static methods and default methods. Below is the example : interface interA{ static void method() { System.out.println("Static method inside interface"); } public default…
Sushmi S
  • 61
  • 1
  • 1
  • 6
1
vote
1 answer

calling a java constructor of same class from non static context leads to recursion but with static it works fine?

I am trying to understand initialization order of Java class. Specifically when and in what order are static and Instance initializer/fields are executed. I came up with example as in this stackoverflow Question. Why does adding static to the self…
1
vote
1 answer

Which classes get initialized when a static method of Base class is called through Derived class reference?

I think that only Base class is initialized when Derived.f() is called. Exactly like it happens when we have a (non-compile-time-constant) static field in Base, not static method. My doubt is only that JLS is not very clear on that. class Base { …
Code Complete
  • 3,146
  • 1
  • 15
  • 38
1
vote
1 answer

Is initializing a static field with a constructor always considered bad practice?

I was reading about initializers in Java. I read almost all the Stackoverflows related questions, and I became quite familiar with what a constructor is ,and, what non-static (instance) initilizer blocks and static initializers are. I think I…
1
vote
3 answers

static arraylist with instances of Class with initial values

Im making a Coin class with a static arraylist that stores every instance of the class created, howevered I need to initiate that list with an initial instance, and I have not figured out how to do it without adding it twice (because of a redundant…
Nicolas Quiroz
  • 367
  • 2
  • 13
1
vote
2 answers

Why static initializer allow re-initialization of static variable in Java?

I am studying static initializers in Java. I came through a source code as given below: public class A { private static int count = 5; final static int STEP = 10; boolean alive; static { count = 1; } …
patrick.1729
  • 4,222
  • 2
  • 20
  • 29
1
vote
1 answer

C++ initialization of struct containing an array

I have a structure that more or less follows this pattern: struct sTruct { int count; struct { int A; int B; int C; } array[]; //count is the size of this array }; I would like to be able to initialize these with…
jkerian
  • 16,497
  • 3
  • 46
  • 59
1
vote
1 answer

Empty std::string in static method initialiser

I am using a static method to initialise the const fields of a class. The static method uses some const variables that are stored in a separate header file. Primitive types are correctly being passed to the static method, but the std::strings are…
user7119460
  • 1,451
  • 10
  • 20
1
vote
2 answers

Static Initialization Before Enum Elements

It is not possible to make a static initialization before initialization of enum elements in Java since enum elements always have to be declared and initialized first. If a static initialization doesn't depend on enum elements, its execution order…
oak
  • 199
  • 1
  • 6
1
vote
2 answers

Why does printing a static variable value gives error in static block while assigning it doesn't

public class ABC { static { System.out.println(i); } static int i=10; static { System.out.println(i); } public static void main(String[] args) { System.out.println(3); ABC a =…
Anuj Singh
  • 19
  • 5