Question:
Is it more expensive to use too many static variables instead of ordinary variables? If yes, then how?
M Azhar javid
2014-07-25 01:56:10 UTC
Is it more expensive to use too many static variables instead of ordinary variables? If yes, then how?
Seven answers:
Malika
2014-08-08 04:46:32 UTC
it would be more expensive to use static variables instead ordinary variables due to the fact that automatic variables are created and destroyed when they go in and out of scope (so memory is freed after the program is out of the Block in which a variable is declared). Whereas static variables are stored until the program terminates – meaning they remain allocated even if you don’t need them in your program after a certain point; which is “wasted” memory.
Bob
2014-07-25 02:12:52 UTC
It's not a problem. However, it is generally accepted good design practice to limit the scope of every variable so that it's as wide as it needs to be - but no wider ;-)



This means that:

- you don't use a method local variable as the controlled variable of a for loop, if you can declare the controlled variable in the for loop heading.

- you don't use a class global variable (field) inside a method if you can instead use a local variable declared inside the method.

- you don't declare a field public if it can be protected, nor protected if it can be private.

- you don't declare a field static if it can be non-static.

- class level "final" values should be declared static.



In my experience as a Java programming teacher, most students have difficulties with the second option, and will tend to declare a lot of class global fields for variables should have been declared local.
?
2014-07-25 03:35:14 UTC
There's no difference in performance between accessing static and non-static variables.
M Azhar javid
2014-07-25 03:16:39 UTC
Sir I just want to know about this question because i am student and i want to write about it. I don't know the answer of it. I am searching answer of this question about 200 words. Please help me. I shall be most thank full to you .
?
2014-07-25 04:53:34 UTC
I think you are the student of software engineering or MCS,BSCS in virtual University of Pakistan.
2014-08-08 00:37:21 UTC
hello i don't know the how to use the cs because i don't read already so pleass help me this question
2014-08-07 00:53:06 UTC
Me too..... suffering with the same problem........ someone help plz......


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...