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
141
votes
9 answers

How can I initialize a C# List in the same line I declare it. (IEnumerable string Collection Example)

I am writing my testcode and I do not want wo write: List nameslist = new List(); nameslist.Add("one"); nameslist.Add("two"); nameslist.Add("three"); I would love to write List nameslist = new List({"one", "two",…
Johannes
  • 6,490
  • 10
  • 59
  • 108
140
votes
8 answers

Set attributes from dictionary in python

Is it possible to create an object from a dictionary in python in such a way that each key is an attribute of that object? Something like this: d = { 'name': 'Oscar', 'lastName': 'Reyes', 'age':32 } e = Employee(d) print e.name # Oscar print…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
131
votes
6 answers

Why can I initialize a List like an array in C#?

Today I was surprised to find that in C# I can do: List a = new List { 1, 2, 3 }; Why can I do this? What constructor is called? How can I do this with my own classes? I know that this is the way to initialize arrays but arrays are…
Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
131
votes
2 answers

In Scala, what exactly does 'val a: A = _' (underscore) mean?

What exactly does val a: A = _ initialize a value to? Is this a typed null? Thanks.
Gregor Scheidt
  • 3,952
  • 4
  • 24
  • 28
130
votes
2 answers

Java: int array initializes with nonzero elements

According to the JLS, an int array should be filled by zeros just after initialization. However, I am faced with a situation where it is not. Such a behavior occurs first in JDK 7u4 and also occurs in all later updates (I use 64-bit implementation).…
Stanislav Poslavsky
  • 2,398
  • 2
  • 24
  • 36
129
votes
15 answers

Why aren't pointers initialized with NULL by default?

Can someone please explain why pointers aren't initialized to NULL? Example: void test(){ char *buf; if (!buf) // whatever } The program wouldn't step inside the if because buf is not null. I would like to know why, in what…
Jonathan
  • 4,724
  • 7
  • 45
  • 65
128
votes
11 answers

How to initialize a private static const map in C++?

I need just dictionary or associative array string => int. There is type map C++ for this case. But I need only one map forall instances(-> static) and this map can't be changed(-> const); I have found this way with boost library std::map
Meloun
  • 13,601
  • 17
  • 64
  • 93
127
votes
10 answers

What is an initialization block?

We can put code in a constructor or a method or an initialization block. What is the use of initialization block? Is it necessary that every java program must have it?
Sumithra
  • 6,587
  • 19
  • 51
  • 50
125
votes
3 answers

When does static initialization happen?

When are static fields initialized? If I never instantiate a class, but I access a static field, are ALL the static blocks and private static methods used to instantiate private static fields called (in order) at that instant? What if I call a…
Tony R
  • 11,224
  • 23
  • 76
  • 101
125
votes
3 answers

Options for initializing a string array

What options do I have when initializing string[] object?
mrblah
  • 99,669
  • 140
  • 310
  • 420
121
votes
5 answers

Cannot instantiate the type List

I have the following code: List product = new List(); The error: Cannot instantiate the type List Product is an Entity in my EJB project. Why I'm getting this error?
Emanuel
  • 6,622
  • 20
  • 58
  • 78
121
votes
5 answers

How do I make a custom initializer for a UIViewController subclass in Swift?

Apologies if this has been asked before, I've searched around a lot and many answers are from earlier Swift betas when things were different. I can't seem to find a definitive answer. I want to subclass UIViewController and have a custom initializer…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
117
votes
1 answer

Array[n] vs Array[10] - Initializing array with variable vs numeric literal

I am having the following issue with my code: int n = 10; double tenorData[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Returns the following error: error: variable-sized object 'tenorData' may not be initialized Whereas using double tenorData[10]…
msmf14
  • 1,449
  • 4
  • 13
  • 19
115
votes
10 answers

Are delphi variables initialized with a value by default?

I'm new to Delphi, and I've been running some tests to see what object variables and stack variables are initialized to by default: TInstanceVariables = class fBoolean: boolean; // always starts off as false fInteger: integer; // always starts…
MB.
  • 7,365
  • 6
  • 42
  • 42
113
votes
9 answers

How to initialize an array in one step using Ruby?

I initialize an array this way: array = Array.new array << '1' << '2' << '3' Is it possible to do that in one step? If so, how?
user502052
  • 14,803
  • 30
  • 109
  • 188