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

Initialize static variables in C++ class?

I have noticed that some of my functions in a class are actually not accessing the object, so I made them static. Then the compiler told me that all variables they access must also be static – well, quite understandable so far. I have a bunch of…
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
112
votes
16 answers

Why are local variables not initialized in Java?

Was there any reason why the designers of Java felt that local variables should not be given a default value? Seriously, if instance variables can be given a default value, then why can't we do the same for local variables? And it also leads to…
Shivasubramanian A
  • 4,346
  • 6
  • 29
  • 26
112
votes
6 answers

initializing a boolean array in java

I have this code public static Boolean freq[] = new Boolean[Global.iParameter[2]]; freq[Global.iParameter[2]] = false; could someone tell me what exactly i'm doing wrong here and how would i correct it? I just need to initialize all the array…
leba-lev
  • 2,788
  • 10
  • 33
  • 43
111
votes
6 answers

Declaring and initializing variables within Java switches

I have a crazy question about Java switches. int key = 2; switch (key) { case 1: int value = 1; break; case 2: value = 2; System.out.println(value); break; default: break; } Scenario 1 -…
ironwood
  • 8,936
  • 15
  • 65
  • 114
110
votes
11 answers

Initialize/reset struct to zero/null

struct x { char a[10]; char b[20]; int i; char *c; char *d[10]; }; I am filling this struct and then using the values. On the next iteration, I want to reset all the fields to 0 or null before I start reusing it. How can I do…
hari
  • 9,439
  • 27
  • 76
  • 110
107
votes
6 answers

C++: Where to initialize variables in constructor

Possible Duplicate: C++ initialization lists What are the pros/cons of initializing variables at option 1 vs option 2? class MyClass { public: MyClass( float f, char a ); private: float mFloat; char mCharacter; bool mBoolean; …
user542687
107
votes
7 answers

Proper way to initialize C++ structs

Our code involves a POD (Plain Old Datastructure) struct (it is a basic c++ struct that has other structs and POD variables in it that needs to get initialized in the beginning.) Based one what I've read, it seems that: myStruct =…
KC3BZU
  • 1,224
  • 2
  • 11
  • 14
106
votes
7 answers

Confusion about array initialization in C

In C language, if initialize an array like this: int a[5] = {1,2}; then all the elements of the array that are not initialized explicitly will be initialized implicitly with zeros. But, if I initialize an array like this: int…
msc
  • 33,420
  • 29
  • 119
  • 214
106
votes
2 answers

Uninitialized Object vs Object Initialized to NULL

I'm working in Java. I commonly setup some objects as such: public class Foo { private SomeObject someName; // do stuff public void someMethod() { if (this.someName != null) { // do some stuff } } } The…
SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
106
votes
7 answers

Initializing entire 2D array with one value

With the following declaration int array[ROW][COLUMN]={0}; I get the array with all zeroes but with the following one int array[ROW][COLUMN]={1}; I don’t get the array with all one value. The default value is still 0. Why this behavior and how…
Kraken
  • 23,393
  • 37
  • 102
  • 162
105
votes
7 answers

Python - Initializing Multiple Lists/Line

This is terribly ugly: psData = [] nsData = [] msData = [] ckData = [] mAData = [] RData = [] pData = [] Is there a way to declare these variables on a single line?
thenickname
  • 6,684
  • 14
  • 41
  • 42
105
votes
2 answers

How to initialize all members of an array to the same value in Swift?

I have a large array in Swift. I want to initialize all members to the same value (i.e. it could be zero or some other value). What would be the best approach?
m_power
  • 3,156
  • 5
  • 33
  • 54
104
votes
7 answers

When are static variables initialized?

I am wondering when static variables are initialized to their default values. Is it correct that when a class is loaded, static vars are created (allocated), then static initializers and initializations in declarations are executed? At what point…
Ankit
  • 1,240
  • 2
  • 13
  • 16
103
votes
8 answers

How to get stage from controller during initialization?

I want to handle stage events (i.e. hiding) from my controller class. So all I have to do is to add a listener like this: ((Stage) myPane.getScene().getWindow()).setOn*whatIwant*(...); But the problem is that initialization starts right after this…
Chechulin
  • 2,426
  • 7
  • 28
  • 35
103
votes
7 answers

(Why) is using an uninitialized variable undefined behavior?

If I have: unsigned int x; x -= x; it's clear that x should be zero after this expression, but everywhere I look, they say the behavior of this code is undefined, not merely the value of x (until before the subtraction). Two questions: Is the…
user541686
  • 205,094
  • 128
  • 528
  • 886