Questions tagged [initialization]

Initialization deals with the task of initializing the contents of your data structure. It's a common practice in statically-typed languages.

Initialization deals with the task of initializing the contents of data structures. This is the initialization of an object, structure, blob of data, or any entity which contains state that must be prepared prior to usage of the entity.

11649 questions
405
votes
8 answers

Declare and initialize a Dictionary in Typescript

Given the following code interface IPerson { firstName: string; lastName: string; } var persons: { [id: string]: IPerson; } = { "p1": { firstName: "F1", lastName: "L1" }, "p2": { firstName: "F2" } }; Why isn't the initialization…
mgs
  • 4,716
  • 2
  • 17
  • 17
391
votes
13 answers

What is Double Brace initialization in Java?

What is Double Brace initialization syntax ({{ ... }}) in Java?
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
376
votes
17 answers

C++ Structure Initialization

Is it possible to initialize structs in C++ as indicated below: struct address { int street_no; char *street_name; char *city; char *prov; char *postal_code; }; address temp_address = { .city = "Hamilton", .prov = "Ontario"…
Dinesh P.R.
  • 6,936
  • 6
  • 35
  • 44
345
votes
26 answers

Difference between declaring variables before or in loop?

I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference? A (quite pointless) example in Java: a) declaration before loop: double…
Rabarberski
  • 23,854
  • 21
  • 74
  • 96
337
votes
12 answers

Initialization of all elements of an array to one default value in C++?

C++ Notes: Array Initialization has a nice list over initialization of arrays. I have a int array[100] = {-1}; expecting it to be full with -1's but its not, only first value is and the rest are 0's mixed with random values. The code int array[100]…
Milan
  • 15,389
  • 20
  • 57
  • 65
322
votes
9 answers

Best way to initialize (empty) array in PHP

In certain other languages (AS3 for example), it has been noted that initializing a new array is faster if done like this var foo = [] rather than var foo = new Array() for reasons of object creation and instantiation. I wonder whether there are any…
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
320
votes
16 answers

DateTime "null" / uninitialized value?

How do you deal with a DateTime that should be able to contain an uninitialized value (equivalent to null)? I have a class which might have a DateTime property value set or not. I was thinking of initializing the property holder to…
Mats
  • 14,902
  • 33
  • 78
  • 110
308
votes
14 answers

Static Initialization Blocks

As far as I understood the "static initialization block" is used to set values of static field if it cannot be done in one line. But I do not understand why we need a special block for that. For example we declare a field as static (without a value…
Roman
  • 124,451
  • 167
  • 349
  • 456
306
votes
8 answers

ArrayList initialization equivalent to array initialization

I am aware that you can initialize an array during instantiation as follows: String[] names = new String[] {"Ryan", "Julie", "Bob"}; Is there a way to do the same thing with an ArrayList? Or must I add the contents individually with array.add()?
Jonathan
  • 3,311
  • 3
  • 22
  • 15
296
votes
4 answers

What is the order of evaluation in a member initializer list?

I have a constructor that takes some arguments. I had assumed that they were initialized in the order listed, but in one case, it appears they were being initialized in reverse, resulting in an abort. When I reversed the arguments, the program…
hookenz
  • 36,432
  • 45
  • 177
  • 286
294
votes
9 answers

Is there a difference between copy initialization and direct initialization?

Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func()); double b1 = 0.5; double b2(0.5); A c1; A c2 = A(); A c3(A()); } In each grouping, are these statements identical? Or is…
rlbond
  • 65,341
  • 56
  • 178
  • 228
269
votes
6 answers

How to call a method after bean initialization is complete?

I have a use case where I need to call a (non-static) method in the bean only-once at the ApplicationContext load up. Is it ok, if I use MethodInvokingFactoryBean for this? Or we have a some better solution? As a side note, I use…
peakit
  • 28,597
  • 27
  • 63
  • 80
267
votes
11 answers

Java: how to initialize String[]?

Error % javac StringTest.java StringTest.java:4: variable errorSoon might not have been initialized errorSoon[0] = "Error, why?"; Code public class StringTest { public static void main(String[] args) { String[]…
hhh
  • 50,788
  • 62
  • 179
  • 282
264
votes
9 answers

Why should I prefer to use member initializer lists?

I'm partial to using member initializer lists for my constructors, but I've long since forgotten the reasons behind this. Do you use member initializer lists in your constructors? If so, why? If not, why not?
oz10
  • 153,307
  • 27
  • 93
  • 128
263
votes
7 answers

Initializing multiple variables to the same value in Java

I'm looking for a clean and efficient method of declaring multiple variables of the same type and of the same value. Right now I have: String one = "", two = "", three = "" etc... But I'm looking for something like: String one,two,three = "" Is…
user83643
  • 2,901
  • 2
  • 17
  • 13