Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not use static array of Strings? [closed]

Tags:

java

standards

Came across this coding standard from Oracle:

Do not use a static array of strings.

What is the reason for this recommendation?

like image 901
Zack Avatar asked Aug 15 '16 17:08

Zack


People also ask

Can we declare array as static in C?

If the array has static storage duration (meaning it was declared at file scope outside of any function body, or was declared with the static keyword) and no initializer is present, then all of the array elements are initialized to 0 (for scalars) or NULL (for pointers).

Do static variables need to be freed?

Static array or variables will not be freed, when control comes out of that function. Scope of static variable is local to the function in which it is declared, but its lifetime is throughout the program.

Do arrays need to be freed?

If the array is declared statically, then we do not need to delete an array since it gets deleted by the end of the program/ block in which it was declared. If the array is declared dynamically, we need to free the memory allocated to it using the free() function.


1 Answers

Arrays are mutable containers. Global mutable container without built-in synchronisation is multi-threaded horror waiting to happen. You can store reference to that array in some local context and then have it's content changed at any time without you knowing it. Or vice versa you may assume that this is convenient global state, but then every client must know the exact correct way to synchronise its access to that state. And somebody will forget and bug will be born.

like image 197
Nikem Avatar answered Sep 28 '22 05:09

Nikem