answersLogoWhite

0


Best Answer

When a variable is declared, your computer assigns a section of memory to that variable. If the variable isn't initialized, there's no way of knowing what data is already stored in that section of memory, which can cause errors in your programs.

In managed languages, such as C#, this is done automatically at declaration. Although it's still good practice to initialize variables yourself.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why static variables are initialized to zero?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Are C variables initialized to 0 by default?

Only global/static variables are, local variables aren't.


Why memory is divided into initialized and uninitialized areas?

The program's data segment. This area of memory is allocated by the linker and is used to store the program's global variables, static variables, static arrays and constants. Constants are always initialised, as are static variables, but global variables and static arrays need not be initialised.


What is the value of the un initialized pointers?

Random garbage, obviously. Let's note that global (ie not automatic) variables are automatically initialized by zero (=NULL)


Can static variables be changed?

No, a static variable means that there is only one copy of that variable, and it is shared by all members of the class, or by all callers of a function.A variable that is read-only would be marked as const or final (depending on language).


What are the value and preference of global variable?

Global variables can have any value, in C they are aumaticatically initialized to zero.


Static method can use only static variable why?

A static method is a method that is a class method and is not attached to the object of that class. So if we use a non static variable of the class, it would most probably not have been initialized because no object could have been created for the class. Hence it would throw a null pointer exception. To avoid such an ambiguity, there is a restriction that static methods can use only static variables. This is to ensure that class methods can access only class variables both of which would get initialized simultaneously.


Can array be initialized if they are static?

Yes.


When an array is declared does c automatically initializes its elements to zero?

For global/static variables: yes.For auto variables: no.


How can a PHP static be initialized?

PHP static can only be initialized using a literal or constant. You can not use an expression. You can initialize it to an integer but you may not to another variable.


Are variables automatically initialized by Java if the programmer does not?

no


Which one executed first static block or static methods in java?

Static Blocks are always executed first. A static block is executed when your class is charged but a static method is executed only when is called, therefor first the class is charged and then is executed a method.


What happen when static data field is initialised in a non static constructor?

It got initialized as you instructed.